保存图片框的图像

时间:2021-10-19 00:24:46

So I have this code:

所以我有这个代码:

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 is a picturebox on my form. PicFinal is another display picturebox. But this code gets me this:保存图片框的图像 Which is basically a screenshot of a window in the size of PicOuterBorder starting from the origin of my screen. However, Me.Bounds instead of PicOuterBorder.Bounds works and gets a perefect screenshot of just my form. I want picFinal to have a screenshot of just PicOuterBorder

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

2 个解决方案

#1


1  

Try below code. You have to map the control coordinates to screen coordinates using PointToScreen. I have placed PicOuterBorder inside the panel PanelPicture. PanelPicture is without any border, while PicOuterBorder can have any type of border style. Below code takes the snapshot of the panel.

试试下面的代码。您必须使用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


1  

Adapt your code for something like this:

调整您的代码,如下所示:

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

#1


1  

Try below code. You have to map the control coordinates to screen coordinates using PointToScreen. I have placed PicOuterBorder inside the panel PanelPicture. PanelPicture is without any border, while PicOuterBorder can have any type of border style. Below code takes the snapshot of the panel.

试试下面的代码。您必须使用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


1  

Adapt your code for something like this:

调整您的代码,如下所示:

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