上色

Code Block

2013年4月8日 星期一

[VB.Net] 將視窗影像截圖並轉存為圖片

Dim saveFileDialog1 As New SaveFileDialog() '儲存檔案公用視窗

Try  '預設錯誤處理方式
    '定義存檔格式
    saveFileDialog1.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|EMF (*.emf)|*.emf|PNG (*.png)|*.png|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif"
    saveFileDialog1.FilterIndex = 2  '預設為第二種(JPG)
    If saveFileDialog1.ShowDialog() = DialogResult.OK Then '使用者按下確認之後紀錄檔名
        GetWindowInfo(Me.Handle)  '以自訂函數GetWindowInfo取得視窗資訊
        '建立一個Bitmap作為存檔目標
        Dim Screenshot As Bitmap = New Bitmap(ClientX, ClientY - 50, PixelFormat.Format32bppArgb)
        Dim picOutput As Graphics = Graphics.FromImage(Screenshot) '建立儲存影像的Graphic
        Dim picSource As Graphics = Graphics.FromHdc(GetDC(Me.Handle)) '建立獲取來源影像的Graphic
        '以Bitbit將來源影像轉存到目標影像
        BitBlt(picOutput.GetHdc(), 0, 0, ClientX, ClientY - 50, picSource.GetHdc(), 0, 50, CopyPixelOperation.SourceCopy) '把來源影像複製到儲存影像中
        '釋放hdc
        picSource.ReleaseHdc()
        picOutput.ReleaseHdc()
        '存為圖片
        Screenshot.Save(saveFileDialog1.FileName)
    End If
Catch ex As Exception
    MsgBox(ex.Message) '錯誤訊息
End Try

**********以下為API宣告**********

'重繪圖檔用API
Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal dwRop As Integer) As Integer

沒有留言:

張貼留言