簡體   English   中英

保存圖片框的圖像

[英]Saving the image of a 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是我表單上的一個PicOuterBorder PicFinal是另一個顯示PicFinal 但是這段代碼讓我明白了: ... 從屏幕的原點開始,這基本上是一個大小為PicOuterBorder的窗口的屏幕截圖。 但是, Me.Bounds代替了PicOuterBorder.Bounds可以正常工作,並且可以獲取我的表單的PicOuterBorder.Bounds屏幕截圖。 我希望picFinal僅具有PicOuterBorder的屏幕截圖

使您的代碼適應如下所示:

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

嘗試下面的代碼。 您必須使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM