Image capture in VB6 and VB.NET
Image capture in VB6
'------------------------------------- ---------------------------
'
'Author: lihonggen0
'Date:
2002-6-19
'Features: Screen
'------------------------------------- ---------------------------
Private Type PointApi
X as long
Y as long
End Type
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal DWROP As long) As long
Private Declare Function Getdc LIB "User32" (BYVAL HWND As Long) AS Long
Private Declare Function GetCursorpos LIB "User32" (LPPOINT AS POINTAPI) AS Long
Private Declare Function Drawicon LIB "User32" (Byval X As Long, Byval Y As Long, BYVAL HICON As Long AS Long
Private Declare Function ReleaseDC LIB "User32" (BYVAL HDC AS Long) AS Long
Private submmand1_click ()
DIM HDC As Long
DIM SW AS INTEGER
DIM SH AS INTEGER
DIM Curpos as Pointapi
DIM CUR As Long
Me.hide
Doevents
Picture1.Autoredraw = TRUE
HDC = getdc (0)
GetCursorpos Curpos
Cur = getCursor
Picture1.width = Screen.width
Picture1.height = Screen.height
SW = Screen.width / Screen.twipsPerpixelx
SH = Screen.height / Screen.TwipsPixely
Bitblt Picture1.hdc, 0, 0, SW, SH, HDC, 0, 0, VBSRCCOPY
Me.show
Drawicon Picture1.hdc, Curpos.x - 10, Curpos.y - 10, CUR
ReleaseDC 0, HDC
Picture1.Autoredraw = false
End Sub
'reference
'http://support.microsoft.com/?kbid=210108http://support.microsoft.com/?id=161299
Image capture in VB.NET, you need to reference some APIs first, the following is a declaration:
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As IntegerPrivate Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
Private Declare Function SelectObject LIB "GDI32" (Byval Hobject As Integer) AS Integer
Private Declare Function BitBlt Lib "GDI32" (ByVal srchDC As Integer, ByVal srcX As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal OP as integer) AS Integer
Private Declare Function Deletedc LIB "GDI32" (Byval HDC AS Integer) AS Integer
Private Declare Function DeleteObject LIB "GDI32" (Byval Hobj AS Integer) AS INTEGER
Declare function getdc lib "user32" alias "getdc" (Byval HWnd as integer) AS Integer
Const srcopy as integer = & hcc0020
Add the following code to the Button1_Click event:
Private sub button1_click (byvale as system.object, byval e as system.eventargs) Handles Button1.click
DIM HDC, HMDC AS INTEGER
DIM HBMP, HBMPOLD AS INTEGER
DIM SW, SH AS INTEGER
HDC = getdc (0)
HMDC = CREATECOMPALEDC (HDC)
SW = Screen.primaryScreen.bounds.width
sh = screen.primaryScreen.Bounds.height
HBMP = CREATECOMPATIBLEBITMAP (HDC, SW, SH)
HBMPOLD = SELECTOBJECT (HMDC, HBMP)
Bitblt (HMDC, 0, 0, SW, SH, HDC, 0, 0, SRCCOPY)
HBMP = SELECTOBJECT (HMDC, HBMPOLD)
Picturebox1.image = image.fromhbitmap (new intptr (HBMP))
DeleteDC (HDC)
Deletedc (HMDC)
DeleteObject (HBMP)
End Sub