There are quite a few ways to capture the screen in VB6. I recommend the following method for several reasons:
- It’s reliable. These APIs offer consistent results (unlike some others).
- It’s simple. Each API call serves a logical purpose, and I’ve detailed those below.
- It’s fast. The code can easily be modified to operate on a timer system, for example.
You can download the sample project, or simply copy-and-paste this code into any blank form:
'Screen Capture Demo by Tanner Helland (published 2008, updated 2012) ' http://www.tannerhelland.com 'If you like VB game and graphics code, be sure to subscribe to my RSS feed at ' http://www.tannerhelland.com/feed/ 'The required API calls are: 'This call gives us the hWnd (window handle) of the screen Private Declare Function GetDesktopWindow Lib "user32" () As Long 'This call assigns an hDC (handle of device context) from an hWnd Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long 'BitBlt lets us draw an image from a hDC to another hDC (in our case, from an hDC of the screen capture ' to the hDC of a VB picture box) Private Declare Function BitBlt Lib "gdi32" (ByVal hDC 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 opCode As Long) As Long 'ReleaseDC will be used to release the screen's hDC once the screen capture is complete. Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long 'This sample project copies the screen when the form loads; you could also place this code in ' a command button (or any other input) Private Sub Form_Load() 'First, minimize this window Me.WindowState = vbMinimized 'Get the hWnd of the screen Dim scrHwnd As Long scrHwnd = GetDesktopWindow 'Now, assign an hDC to the hWnd we generated Dim shDC As Long shDC = GetDC(scrHwnd) 'Determine the size of the screen Dim screenWidth As Long, screenHeight As Long screenWidth = Screen.Width \ Screen.TwipsPerPixelX screenHeight = Screen.Height \ Screen.TwipsPerPixelY 'Copy the data from the screen hDC to this VB form BitBlt FormScreenCapture.hDC, 0, 0, screenWidth, screenHeight, shDC, 0, 0, vbSrcCopy 'Release our hold on the screen's hDC ReleaseDC scrHwnd, shDC 'Set the picture of the form to equal its image FormScreenCapture.Picture = FormScreenCapture.Image 'Restore the window Me.WindowState = vbNormal End Sub