从picturebox保存图像 - VB

时间:2014-07-27 17:20:47

标签: vb.net screenshot system.drawing

我试图组合两个代码(button2和button3)。我想在Button2点击图片框1中的图像进行压缩(button3代码)并保存而不进行对话,只需保存即可。这是代码(button2代码工作,但是给出对话框,button3给出错误)。

错误: 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

提前致谢!

2 个答案:

答案 0 :(得分:0)

我从您的问题中了解到,您必须在Button_3下删除代码后单击Button_2,保存捕获的ScreenShot cliking Button_1而不显示dialoge。如果喜欢的话 -

按照这种方式.........

首先从您的代码中删除Button_1 Sub中的 Dim screenshot As System.Drawing.Bitmap并将其粘贴为文档的通用。

其次只需复制以下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

第三,完全保留Button_2下的代码并剪切&将代码粘贴到Button_2

下的Button_3下

像这样快速改变你的代码。

 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

剩下的所有代码仍然存在。

现在完成!

以下是示例图片。

enter image description here

答案 1 :(得分:0)

在不询问对话框的情况下保存pictureBox的可见部分...

只需将以下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

然后在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\1" & ".jpg", bm)

'// Save The image in PictureBox in its Original size.
    SaveImage("C:\Users\User\Desktop\1" & ".jpg", PictureBox1.image)