代码之家  ›  专栏  ›  技术社区  ›  user3077703

从picturebox保存图像-VB

  •  0
  • user3077703  · 技术社区  · 10 年前

    我试图组合两个代码(按钮2和按钮3)。我希望当单击按钮2时,图片框1中的图像将被压缩(按钮3代码)并在没有对话框的情况下保存,只需保存而无需询问。下面是代码(button2代码有效,但给出对话框,button3给出错误)。

    错误: System.Drawing.dll中发生“System.ArgumentException”类型的未处理异常 其他信息:参数无效。LINE:将bmp1变暗为新位图(“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 回复  |  直到 10 年前
        1
  •  0
  •   Koyas    10 年前

    我从你的问题中了解到,在退出Button_3下的代码后,你必须通过单击Button_2保存捕获的ScreenShot cliking Button_1而不显示对话框。如果是这样的话--

    沿着这条路。。。。。。。。。

    一开始 从代码中释放 Dim screenshot As System.Drawing.Bitmap 从Button_1 Sub,并将其粘贴为文档的泛型。

    其次 只需复制以下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_3下的代码粘贴到Button_2下

    立即像这样更改代码。

     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

        2
  •  0
  •   Koyas    10 年前

    保存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)