【搬運】VB.NET屏幕截圖方法2則
' 根據VB6代碼改的,經過本人優化。請先導入Drawing類庫。
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Integer, ByVal hObject As Integer) As Integer
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
Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByRef lpInitData As Integer) As Integer
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Integer) As Integer
Function GetSerPic(Optional ByVal BitWidth As Integer = -1, Optional ByVal BitHeight As Integer = -1) As Image
If BitWidth < 0 Then BitWidth = My.Computer.Screen.Bounds.Width
If BitHeight < 0 Then BitHeight = My.Computer.Screen.Bounds.Height
Dim Bhandle, DestDC, SourceDC As IntPtr
SourceDC = CreateDC("DISPLAY", Nothing, Nothing, 0)
DestDC = CreateCompatibleDC(SourceDC)
Bhandle = CreateCompatibleBitmap(SourceDC, BitWidth, BitHeight)
SelectObject(DestDC, Bhandle)
BitBlt(DestDC, 0, 0, BitWidth, BitHeight, SourceDC, 0, 0, &HCC0020)
Return Image.FromHbitmap(Bhandle)
End Function
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Integer, ByVal hObject As Integer) As Integer
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
Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByRef lpInitData As Integer) As Integer
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Integer) As Integer
Function GetSerPic(Optional ByVal BitWidth As Integer = -1, Optional ByVal BitHeight As Integer = -1) As Image
If BitWidth < 0 Then BitWidth = My.Computer.Screen.Bounds.Width
If BitHeight < 0 Then BitHeight = My.Computer.Screen.Bounds.Height
Dim Bhandle, DestDC, SourceDC As IntPtr
SourceDC = CreateDC("DISPLAY", Nothing, Nothing, 0)
DestDC = CreateCompatibleDC(SourceDC)
Bhandle = CreateCompatibleBitmap(SourceDC, BitWidth, BitHeight)
SelectObject(DestDC, Bhandle)
BitBlt(DestDC, 0, 0, BitWidth, BitHeight, SourceDC, 0, 0, &HCC0020)
Return Image.FromHbitmap(Bhandle)
End Function
' 最近學了Graphics類的畫圖方法,所以無聊就寫了兩個小程序。
' 這一例是不使用系統API就可以進行屏幕截取的代碼。
Public Class frmMain
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
Dim p1 As New Point(0, 0)
Dim p2 As New Point(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim pic As New Bitmap(p2.X, p2.Y)
Using g As Graphics = Graphics.FromImage(pic)
g.CopyFromScreen(p1, p1, p2)
Me.BackgroundImage = pic
End Using
Me.Show()
End Sub
End Class
' 這一例是不使用系統API就可以進行屏幕截取的代碼。
Public Class frmMain
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
Dim p1 As New Point(0, 0)
Dim p2 As New Point(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim pic As New Bitmap(p2.X, p2.Y)
Using g As Graphics = Graphics.FromImage(pic)
g.CopyFromScreen(p1, p1, p2)
Me.BackgroundImage = pic
End Using
Me.Show()
End Sub
End Class