vb.net VB:如何在以下vb代码中将图像保存到文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23913533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB: How to save image to folder in the following vb code?
提问by user3639723
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..?
在上面的代码中,我想使用自定义文件名将图像保存到 ac:/ 驱动器,并替换默认文件夹“\bin”和名称“\test”。在上面的代码中......使用自定义目标和文件名选项保存图像的正确代码是什么......?
Thank U
感谢你
回答by Stella
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")
您的图像将使用字符串“file_name”包含的值的名称保存。(加上字符串“bmp”)
So you should assign the path + file name you want to use to the string that's currently used to save your image.
因此,您应该将要使用的路径 + 文件名分配给当前用于保存图像的字符串。
So replace
所以更换
Dim file_name As String = Application.ExecutablePath
file_name = file_name.Substring(0, _
file_name.LastIndexOf("\bin")) & _
"\test."
with
和
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;
回答by porkchop
Using Image.Savemethod: http://msdn.microsoft.com/en-us/library/ktx83wah%28v=vs.110%29.aspx
使用Image.Save方法: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
或者如果您有对象的字节流,则使用文件流: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

