user3077703 user3077703 - 5 months ago 129
Vb.net Question

Save image from picturebox - VB

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).

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")

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!

Answer

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 --

follow this way.........

At first From your code just free out the Dim screenshot As System.Drawing.Bitmap from Button_1 Sub and paste it as generic for document.

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

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.

enter image description here

Comments