VB.NET屏幕截图方法2则

时间:2022-03-17 02:04:53

【搬运】VB.NET屏幕截图方法2则

VB.NET屏幕截图方法2则

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
VB.NET屏幕截图方法2则

 

VB.NET屏幕截图方法2则
' 最近学了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
VB.NET屏幕截图方法2则