2017-04-11 66 views
0

的图像所以我有这样的代码:保存一个PictureBox

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的作品,并得到了我的形式perefect截图。我想picFinal拥有的只是PicOuterBorder

回答

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 
1

适应你的代码是这样的截图:

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

但首先我需要适应自己,以了解你所说的。请澄清。 – TGamer