VB: ¿Cómo guardar la imagen en la carpeta en el siguiente código vb?

Public Class Form1
    'Webcam
    Public Touchless As New TouchlessLib.TouchlessMgr
    Public Camera1 As TouchlessLib.Camera = Touchless.Cameras.ElementAt(1)
    Public Camera2 As TouchlessLib.Camera = Touchless.Cameras.ElementAt(0)

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    PictureBox3.Image = Touchless.Cameras.ElementAt(1).GetCurrentImage

    PictureBox4.Image = Touchless.Cameras.ElementAt(0).GetCurrentImage

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Touchless.CurrentCamera = Camera1
    Touchless.CurrentCamera.CaptureHeight = 250
    Touchless.CurrentCamera.CaptureWidth = 300
    Touchless.CurrentCamera = Camera2
    Touchless.CurrentCamera.CaptureHeight = 250
    Touchless.CurrentCamera.CaptureWidth = 300
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    PictureBox1.Image = Touchless.Cameras.ElementAt(1).GetCurrentImage

    PictureBox2.Image = Touchless.Cameras.ElementAt(0).GetCurrentImage
End Sub

' Save the picture.
Private Sub Button2_Click(ByVal sender As  _
    System.Object, ByVal e As System.EventArgs) Handles _
    Button2.Click
    ' Compose the picture's base file name.
    Dim file_name As String = Application.ExecutablePath
    file_name = file_name.Substring(0, _
        file_name.LastIndexOf("\bin")) & _
        "\test."

    ' Get a Bitmap.
    Dim bm As Bitmap = PictureBox4.Image

    ' Save the picture as a bitmap, JPEG, and GIF.
    bm.Save(file_name & "bmp", _
        System.Drawing.Imaging.ImageFormat.Bmp)
    bm.Save(file_name & "jpg", _
        System.Drawing.Imaging.ImageFormat.Jpeg)
    bm.Save(file_name & "gif", _
        System.Drawing.Imaging.ImageFormat.Gif)

    MsgBox("Ok")
End Sub

In the above code, i want to save image to a c:/ drive with a custom file name and with replace of default folder "\bin" and name "\test." in the above code...what is the correct code to save image with custom destination & file name option..?

Gracias

preguntado el 28 de mayo de 14 a las 14:05

2 Respuestas

Take a look at the following part of the sample you posted.

bm.Save(file_name & "bmp", _System.Drawing.Imaging.ImageFormat.Bmp)

Your image will be saved with the name of the value that the string "file_name" holds. (plus the string "bmp")

So you should assign the path + file name you want to use to the string that's currently used to save your image.

Así que reemplaza

Dim file_name As String = Application.ExecutablePath
    file_name = file_name.Substring(0, _
        file_name.LastIndexOf("\bin")) & _
        "\test."

file_name = "C:\yourFileNameHere."

Then you can save your image using

bm.Save(file_name & ".bmp", _System.Drawing.Imaging.ImageFormat.Bmp)

If you want to give the name from within your form you could use a textbox and pass the text from the textbox to the string

file_name = TextBox1.Text;

contestado el 28 de mayo de 14 a las 15:05

Usar Image.Save método: http://msdn.microsoft.com/en-us/library/ktx83wah%28v=vs.110%29.aspx

or using filestream if you have a bytestream of the object: http://msdn.microsoft.com/en-us/library/vstudio/system.io.filestream

Using FS As New IO.FileStream("C:\location\file.ext", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
    FS.Write(item.imageBytes, 0, item.imageBytesLength)
End Using

to get a bytestream from an image, use:

Using FS As New IO.FileStream("C:\location\file.ext", IO.FileMode.Open, IO.FileAccess.Read)
    Dim _theBytes(FS.Length) As Byte
    FS.Read(_theBytes, 0, FS.Length)
    _imageBytes = _theBytes
End Using

contestado el 28 de mayo de 14 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.