Click here to Skip to main content
Email Password   helpLost your password?

Introduction

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

Background

For one of my application, there was need to take screenshot of active window, I searched net but couldn’t find any simple and easy code to do same. There were few ones but were too easy to understand, hence thought of writing my own code and here is the outcome.

Download

Demo applicaion with full source code can be downloaded from here .

RNSoftech.jpg

Using the code

After exploring Windows API, I found this task can be done with the help of built-in functions in API.

Some of the functions used are :

  1. GetWindowDc - retrieves device context of window
  2. CreateCompatibleDc - creates memory device context (DC) compatible with the specified device
  3. BitBlt - Perform pixel level transfer of data from one DC to another.

Using Windows API in Visual Basic: Before using Windows API functions they are declared either as private or public functions as shown in example below

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


For exact syntax of declaration use “API Viewer” tool shipped with Visual Basic.

Code Algoritham explanation in brief :

  1. Width and height of specified window is evaluated
  2. Device context of specified window is retrieved
  3. A new device context (DC) compatible with specified window is created in memory DC.
  4. A bitmap object of size equal to specified window is created in memory DC
  5. Image pixels are copied from Window DC to memory DC using BitBlt function.
  6. Ole picture object is created and saved to file using SavePicture function.
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
    
ErrorHandler:
    GetWindowScreenshot = 0
End Function

Download

Demo applicaion with full source code can be downloaded from here .

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralPrintWindow
leppie
22:28 18 Jun '08  
is a lot easier...

xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008)

Generalstrange formatting [modified]
Corinna John
21:56 18 Jun '08  
The CP template contains a sans-serif font for the article text. Below your code snippet the text looks alright, but the upper part of your article looks really strange.

[EDIT]Ah, I see, you've just fixed it. Rose [/EDIT]

This statement is false.
modified on Thursday, June 19, 2008 6:55 AM


Last Updated 19 Jun 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010