VB:如何在以下vb代码中将图像保存到文件夹?

时间:2014-05-28 13:50:00

标签: vb.net visual-studio-2010

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

在上面的代码中,我想用自定义文件名将图像保存到c:/驱动器,并替换默认文件夹“\ bin”并命名为“\ test”。在上面的代码中...使用自定义目的地&保存图像的正确代码是什么?文件名选项..?

谢谢你

2 个答案:

答案 0 :(得分:0)

查看您发布的示例的以下部分。

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

您的图片将以字符串" file_name"的值名称保存。成立。 (加上字符串" bmp")

因此,您应该将要使用的路径+文件名分配给当前用于保存图像的字符串。

所以替换

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

file_name = "C:\yourFileNameHere."

然后您可以使用

保存图像
bm.Save(file_name & ".bmp", _System.Drawing.Imaging.ImageFormat.Bmp)

如果您想在表单中提供名称,可以使用文本框并将文本框中的文本传递给字符串

file_name = TextBox1.Text;

答案 1 :(得分:0)

使用Image.Save方法:http://msdn.microsoft.com/en-us/library/ktx83wah%28v=vs.110%29.aspx

或使用文件流,如果您有对象的字节流: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

从图像中获取字节流,请使用:

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