2017-04-11 32 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