vb.net 从图片框保存图像 - VB
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24983498/
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
Save image from picturebox - VB
提问by user3077703
Im trying to combine two codes (button2 and button3). I want when Button2 is clicked the image from picturebox1 to be compressed (button3 code) and saved without dialog, just save without asking. Here is the code(button2 code works, but is giving dialog, button3 gives an error).
我试图组合两个代码(button2 和 button3)。我希望当单击 Button2 时,picturebox1 中的图像被压缩(button3 代码)并在没有对话框的情况下保存,无需询问即可保存。这是代码(button2 代码有效,但给出对话框,button3 给出错误)。
THE ERROR: An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll Additional information: Parameter is not valid. LINE: Dim bmp1 As New Bitmap("c:\TestPhoto.jpg")
错误:System.Drawing.dll 中发生类型为“System.ArgumentException”的未处理异常附加信息:参数无效。LINE: Dim bmp1 As New Bitmap("c:\TestPhoto.jpg")
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = Screen.PrimaryScreen.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
PictureBox1.Image = screenshot
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim savefiledialog1 As New SaveFileDialog
savefiledialog1.Title = "Save File"
savefiledialog1.FileName = "*.jpg"
savefiledialog1.Filter = "Jpeg |*.jpg"
If savefiledialog1.ShowDialog() = DialogResult.OK Then
PictureBox1.Image.Save(savefiledialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Private Sub VaryQualityLevel()
' Get a bitmap.
Dim bmp1 As New Bitmap("c:\TestPhoto.jpg")
Dim jgpEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
Dim myEncoderParameters As New EncoderParameters(1)
Dim myEncoderParameter As New EncoderParameter(myEncoder, 50&)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters)
myEncoderParameter = New EncoderParameter(myEncoder, 100&)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("c:\TestPhotoQualityHundred.jpg", jgpEncoder, myEncoderParameters)
' Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = New EncoderParameter(myEncoder, 0&)
myEncoderParameters.Param(0) = myEncoderParameter
bmp1.Save("c:\TestPhotoQualityZero.jpg", jgpEncoder, myEncoderParameters)
End Sub 'VaryQualityLevel
Private Function GetEncoder(ByVal format As ImageFormat) As ImageCodecInfo
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageDecoders()
Dim codec As ImageCodecInfo
For Each codec In codecs
If codec.FormatID = format.Guid Then
Return codec
End If
Next codec
Return Nothing
End Function
Thanks in advance!
提前致谢!
采纳答案by Koyas
I understand from your question, you have to save the captured ScreenShot cliking Button_1 without showing dialoge by clicking Button_2 after exicuting code under Button_3. If it like so --
我从您的问题中了解到,您必须在 Button_3 下执行代码后单击 Button_2 来保存捕获的 ScreenShot 点击 Button_1 而不显示对话框。如果是这样的话——
follow this way.........
跟着这条路…………
At first From your code just free out theDim screenshot As System.Drawing.Bitmapfrom Button_1 Sub and paste it as generic for document.
起初,从您的代码中释放Dim screenshot As System.Drawing.Bitmapfrom Button_1 Sub 并将其粘贴为文档的通用代码。
Secondly Just copy the following Sub.
其次只需复制以下子。
Public Sub SaveImage(filename As String, image As Image, Encoder As ImageCodecInfo, EncParam As EncoderParameter)
Dim path As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, filename & ".jpg")
Dim mySource As New Bitmap(image.Width, image.Height)
Dim grfx As Graphics = Graphics.FromImage(mySource)
grfx.DrawImageUnscaled(image, Point.Empty)
grfx.Dispose()
mySource.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
mySource.Dispose()
End Sub
Thirdly Leave completely the code under Button_2 and cut & paste the code under Button_3 under Button_2
第三,将Button_2下的代码完全保留,将Button_3下的代码剪切粘贴到Button_2下
Forthly chnge your Code like this.
像这样改变你的代码。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Private Sub VaryQualityLevel()
' Get a bitmap.
Dim bmp1 As New Bitmap(screenshot)
Dim jgpEncoder As ImageCodecInfo = GetEncoder(ImageFormat.Jpeg)
Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
Dim myEncoderParameters As New EncoderParameters(1)
Dim myEncoderParameter As New EncoderParameter(myEncoder, 50&)
myEncoderParameters.Param(0) = myEncoderParameter
'your Code....// bmp1.Save("c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters)
'// Call the above Sub
SaveImage("C:\Users\User\Desktop\TestPhotoQualityFifty.jpg", bmp1, jgpEncoder, myEncoderParameter)
myEncoderParameter = New EncoderParameter(myEncoder, 100&)
myEncoderParameters.Param(0) = myEncoderParameter
'your Code....// bmp1.Save("c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters)
'// Call the above Sub
SaveImage("C:\Users\User\Desktop\TestPhotoQualityFifty.jpg", bmp1, jgpEncoder, myEncoderParameter)
' Save the bitmap as a JPG file with zero quality level compression.
myEncoderParameter = New EncoderParameter(myEncoder, 0&)
myEncoderParameters.Param(0) = myEncoderParameter
'your Code....// bmp1.Save("c:\TestPhotoQualityFifty.jpg", jgpEncoder, myEncoderParameters)
'// Call the above Sub
SaveImage("C:\Users\User\Desktop\TestPhotoQualityFifty.jpg", bmp1, jgpEncoder, myEncoderParameter)
End Sub 'VaryQualityLevel
Rest of all your code remains.
剩下的所有代码都保留了下来。
And Now Done!
现在完成了!
Here is the sample Image.
这是示例图像。


回答by Koyas
To Save the Visible part of a pictureBox without asking dialog ...
要在不询问对话框的情况下保存图片框的可见部分...
Just Copy paste the following Sub to your code
只需将以下 Sub 复制粘贴到您的代码中
Public Sub SaveImage(filename As String, image As Image)
Dim path As String = System.IO.Path.Combine(My.Application.Info.DirectoryPath, filename & ".jpg")
Dim mySource As New Bitmap(image.Width, image.Height)
Dim grfx As Graphics = Graphics.FromImage(mySource)
grfx.DrawImageUnscaled(image, Point.Empty)
grfx.Dispose()
mySource.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg)
mySource.Dispose()
End Sub
Then add this code under Button or other exicutable place.
然后在 Button 或其他可执行的地方添加此代码。
Dim x As Integer = PictureBox1.Width
Dim y As Integer = PictureBox1.Height
Dim bm As New Bitmap(x, y)
PictureBox1.DrawToBitmap(bm, New Rectangle(0, 0, x, y))
PictureBox1.Image = bm
'// Save The Visible Part of PictureBox Only (.....Cropped.....)
SaveImage("C:\Users\User\Desktop" & ".jpg", bm)
'// Save The image in PictureBox in its Original size.
SaveImage("C:\Users\User\Desktop" & ".jpg", PictureBox1.image)

