Click here to Skip to main content
14,639,649 members
Articles » Languages » VB6 Interop » General
Posted 25 Jun 2008


31 bookmarked

Capturing Screenshot of Desktop Programatically in an Easy Way in Visual Basic

Rate this:
3.76 (12 votes)
Please Sign up or sign in to vote.
3.76 (12 votes)
11 Oct 2009CPOL
The code shows an easy way to capture desktop to file.


This code snippet illustrates how to capture desktop or any window to a BMP file with minimum coding. It also demonstrates a neat way to save device context (DC) to a file on hard disk.

Code Explanation

To capture window, we can make use of built in functions available in Windows API. To get a list of functions available in this API, use API Viewer tool which can be added to Visual studio from Add-Ins Manager. Here is the list of some functions used from API:

  • GetWindowDc - Retrieves device context of DC
  • CreateCompatibleDc - Creates device context in memory
  • BitBlt - Performs pixel level transfer of data from one DC to other

How to Use API Functions in Visual Basic?

API functions are required to be declared before they are used. The best way is to declare all the required functions in a module file so that they are accessible to any form. Example of declaration:

Private Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" 
	(ByVal hwnd As Long) As Long

For declaration syntax of all other functions, please use “API Viewer” tool.

Code Algorithm Explanation in Brief

  • Calculate width and height of window to copied
  • Retrieve device context known as DC of window
  • Create a new device context (DC) using API function (CreateCompatibleDC) in memory
  • Create a bitmap object using API in newly created DC
  • Copy image data from window DC to Memory DC using API function BitBlt
  • From the bitmap object, create Ole picture object
  • Save Olepicture to file using VB function saveimage
Public Function GetWindowScreenshot_
    (WndHandle As Long, SavePath As String, Optional BringFront As Integer = 1) As Long
' Function to create screeenshot of specified window and store at specified path
    On Error GoTo ErrorHandler

    Dim hDCSrc As Long
    Dim hDCMemory As Long
    Dim hBmp As Long
    Dim hBmpPrev As Long
    Dim WidthSrc As Long
    Dim HeightSrc As Long
    Dim Pic As PicBmp
    Dim IPic As IPicture
    Dim IID_IDispatch As guid
    Dim rc As RECT
    Dim pictr As PictureBox    
    'Bring window on top of all windows if specified
    If BringFront = 1 Then BringWindowToTop WndHandle
    'Get Window Size
    GetWindowRect WndHandle, rc
    WidthSrc = rc.Right - rc.Left
    HeightSrc = rc.Bottom - rc.Top
    'Get Window  device context
    hDCSrc = GetWindowDC(WndHandle)
    'create a memory device context
    hDCMemory = CreateCompatibleDC(hDCSrc)
    'create a bitmap compatible with window hdc
    hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
    'copy newly created bitmap into memory device context
    hBmpPrev = SelectObject(hDCMemory, hBmp)    
    'copy window window hdc to memory hdc
    Call BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, _
                hDCSrc, 0, 0, vbSrcCopy)
    'Get Bmp from memory Dc
    hBmp = SelectObject(hDCMemory, hBmpPrev)
    'release the created objects and free memory
    Call DeleteDC(hDCMemory)
    Call ReleaseDC(WndHandle, hDCSrc)
    'fill in OLE IDispatch Interface ID
    With IID_IDispatch
       .data1 = &H20400
       .data4(0) = &HC0
       .data4(7) = &H46
     End With
    'fill Pic with necessary parts
    With Pic
       .Size = Len(Pic)         'Length of structure
       .Type = vbPicTypeBitmap  'Type of Picture (bitmap)
       .hBmp = hBmp             'Handle to bitmap
       .hPal = 0&               'Handle to palette (may be null)
     End With
    'create OLE Picture object
    Call OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
    'return the new Picture object
    SavePicture IPic, SavePath
    GetWindowScreenshot = 1
    Exit Function
    GetWindowScreenshot = 0
End Function

More Graphics Related Code

Many more easy to use and free code snippets in VB 6 are available here.

Download Source Code with Demo Application

The demo application with full source code can be downloaded from here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rajesh Naik Ponda Goa
Web Developer
India India
I'm a software developer from Goa, India & I take assignments of software development. My specialization is in desktop applications built using latest technologies such as PRISM, WPF, WCF etc

Comments and Discussions

QuestionUsing VB to copy contents of a window to a excel file Pin
Ravinder Choudhary13-May-10 9:00
MemberRavinder Choudhary13-May-10 9:00 
Generalscreen Pin
Jufine11-Nov-09 22:27
MemberJufine11-Nov-09 22:27 
GeneralGood Pin
Md. Marufuzzaman11-Oct-09 19:08
professionalMd. Marufuzzaman11-Oct-09 19:08 
GeneralMy vote of 2 Pin
Dave Kreskowiak11-Oct-09 6:50
mveDave Kreskowiak11-Oct-09 6:50 
General"I started out as a child....." Pin
tonyt11-Apr-10 12:07
Membertonyt11-Apr-10 12:07 
GeneralScreenshot of the entire screen Pin
jan wilmans24-Jul-08 1:49
Memberjan wilmans24-Jul-08 1:49 
GeneralRe: Screenshot of the entire screen Pin
Rajesh Naik Ponda Goa11-Oct-09 0:56
MemberRajesh Naik Ponda Goa11-Oct-09 0:56 
GeneralRe: Screenshot of the entire screen Pin
vbguyny16-Feb-11 8:54
Membervbguyny16-Feb-11 8:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.