How to properly capture the screen in VB6

There are quite a few ways to capture the screen in VB6. I recommend the following method for several reasons:

  1. It’s reliable.  These APIs offer consistent results (unlike some others).
  2. It’s simple.  Each API call serves a logical purpose, and I’ve detailed those below.
  3. 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

Similar Posts:

 

DISCLAIMER: These download files are regularly scanned to ensure they remain free from malicious content. Unfortunately, some virus scanners will flag these .zip files as suspicious simply because they contain source code and/or executable files. I have submitted my projects to a number of companies in an attempt to rectify these false-positives. Some have been cooperative. Others have not. If your virus scanner alerts you regarding these files, please allow the file to be submitted for further analysis (if your program allows for that). This should help ensure that any false-positive warnings gradually disappear for all users.

This site - and its many free downloads - are 100% funded by donations. Please consider a small contribution to fund server costs and to help me support my family. Even $1.00 helps. Thank you!

3 thoughts on “How to properly capture the screen in VB6”

  1. Hi, Great Site.

    I like your Screen Capture Demo

    Adding the following code allows saving the image to a bmp file..
    (placed after the line FormScreenCapture.Picture =FormScreenCapture.Image)

    ‘…Save the picture to a file
    Dim Mysavefile_spec As String
    Mysavefile_spec = “C:\Fred.bmp”
    Call SavePicture(Me.Picture, Mysavefile_spec)

    1. Hi William. The GetPixel declaration isn’t necessary for screen capture, so I should remove it from the downloadable copy of the code. Thanks much for letting me know about this!

Leave a Reply