5,696,576 members and growing! (19,202 online)
Email Password   helpLost your password?
Languages » VB6 Interop » General     Intermediate License: The Code Project Open License (CPOL)

Capturing screenshot of Desktop programatically in easy way in visual basic

By Rajesh Naik Ponda Goa

Code shows easy way to capture desktop to file
VB (VB 6, VB), Windows (Windows, Win2K, WinXP, Win2003, Vista), WinForms, Dev

Posted: 25 Jun 2008
Updated: 25 Jun 2008
Views: 4,744
Bookmarked: 9 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.62 Rating: 3.75 out of 5
1 vote, 20.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 20.0%
4
3 votes, 60.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

IntroductionThis 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 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. Best way is to declare all the required function 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 Algoritham 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
    
ErrorHandler:
    GetWindowScreenshot = 0
End Function

More Graphics related code: Many more easy to use and free code snippets in vb6 availale here


Download Source Code with Demo Application
Demo application with full source code can be downloaded from here .

License

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


RN Softech is a goan software development company, based at Goa, India.
Occupation: Web Developer
Company: www.rnsoftech.com
Location: India India

Other popular VB6 Interop articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralScreenshot of the entire screenmemberjan wilmans2:49 24 Jul '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Jun 2008
Editor:
Copyright 2008 by Rajesh Naik Ponda Goa
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project