【问题标题】:Saving the image of a picturebox保存图片框的图像
【发布时间】:2017-04-11 19:51:26
【问题描述】:

所以我有这个代码:

Private Sub button28_Click(sender As Object, e As EventArgs) Handles button28.Click
    Dim bounds As Rectangle
    Dim screenshot As System.Drawing.Bitmap
    Dim graph As Graphics
    bounds = PicOuterBorder.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)
    picFinal.Image = screenshot
    'this takes a screenshot
End Sub 

PicOuterBorder 是我表单上的图片框。 PicFinal 是另一个显示图片框。但是这段代码让我知道: 这基本上是从我的屏幕原点开始的 PicOuterBorder 大小的窗口的屏幕截图。但是,Me.Bounds 而不是PicOuterBorder.Bounds 可以正常工作,并获得我表单的完美截图。我希望 picFinal 有一个 PicOuterBorder 的屏幕截图

【问题讨论】:

    标签: vb.net picturebox


    【解决方案1】:

    试试下面的代码。您必须使用PointToScreen 将控件坐标映射到屏幕坐标。我已将PicOuterBorder 放在面板PanelPicture 内。 PanelPicture 没有任何边框,而PicOuterBorder 可以有任何类型的边框样式。下面的代码拍摄了面板的快照。

    Private Sub button28_Click(sender As Object, e As EventArgs) Handles button28.Click
        Dim graph As Graphics = Nothing
        Dim bounds As Rectangle = Nothing
        Dim screenshot As System.Drawing.Bitmap
    
        Dim location As Drawing.Point = PanelPicture.PointToScreen(Drawing.Point.Empty)
        screenshot = New System.Drawing.Bitmap(PanelPicture.Width, PanelPicture.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        graph = Graphics.FromImage(screenshot)
        graph.CopyFromScreen(location.X, location.Y, 0, 0, PanelPicture.Size, CopyPixelOperation.SourceCopy)
        picFinal.Image = screenshot
    
        graph.Dispose()
    End Sub
    

    【讨论】:

      【解决方案2】:

      调整您的代码以适应这样的情况:

      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
      

      【讨论】:

      • 但首先我需要调整自己以理解您所说的内容。请澄清一下。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-22
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多