Capture Screen Image???

Posted on 1998-04-12
Last Modified: 2012-06-22
Hi experts,
I am trying to make an application in which i want to capture the screen image of whatever is placed on the screen behind my application.  I have tried to use the BitBlt function but since i do not have any documentation on it i am unable to use it - can anyone give me a sample code of capturing the screen image.  Also is it possible to save that image in .bmp or .jpg format so that anyone on the net can read it and display it on their own screen.

A very quick response will be much appriciated since i am getting extrememly close to my deadline.
Question by:msali
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4

Expert Comment

ID: 1461073
This is not what you asked for, but may be useful anyway.  If you press Alt-PrintScreen, a copy of the currently active window is copied to the clipboard.

Accepted Solution

SGC earned 40 total points
ID: 1461074
This is from the VBKB that comes with VB3. You'll need to make whatever adjustments to the API if your using VB432 or VB5.

How to Copy Entire Screen into a Picture Box in VB for Windows
Article ID: Q80670


Using the Windows API call BitBlt, you can capture the entire
Microsoft Windows screen and place the image into a Microsoft Visual
Basic for Windows picture box. You first get the handle to the desktop,
then use the desktop window handle to get the handle to the desktop's
device context (hDC), and finally use the Windows API call BitBlt to
copy the screen into the Picture property of a Visual Basic for
Windows picture box control.

More Information:


1. Start Visual Basic for Windows (VB.EXE). Form1 is created by default.

2. Create a picture box (Picture1) on Form1.

3. Set the following properties:

   Control   Property      Value
   -------   --------      -----

   Picture1  AutoRedraw    True
   Picture1  Visible       False

4. Add the following code:


Type lrect
    left As Integer
    top As Integer

    right As Integer
    bottom As Integer
End Type
Declare Function GetDesktopWindow Lib "user" () As Integer
Declare Function GetDC Lib "user" (ByVal hWnd%) As Integer

' Note: The following Declare should be on one line:
Declare Function BitBlt Lib "GDI" (ByVal hDestDC%,
                                   ByVal X%,
                                   ByVal Y%,
                                   ByVal nWidth%,
                                   ByVal nHeight%,
                                   ByVal hSrcDC%,
                                   ByVal XSrc%,
                                   ByVal YSrc%,
                                   ByVal dwRop&
                                  ) As Integer

' Note: The following Declare should be on one line:
Declare Function ReleaseDC Lib "User"(ByVal hWnd As Integer,
                                      ByVal hDC As Integer
                                     ) As Integer

Declare Sub GetWindowRect Lib "User" (ByVal hWnd%, lpRect As lrect)
Global Const True = -1
Global Const False = 0
Global TwipsPerPixel As Single


Sub Form_Click ()
  Call GrabScreen
End Sub

Sub GrabScreen ()

    Dim winSize As lrect

    ' Assign information of the source bitmap.
    ' Note that BitBlt requires coordinates in pixels.
    hwndSrc% = GetDesktopWindow()
    hSrcDC% = GetDC(hwndSrc%)
    XSrc% = 0: YSrc% = 0
    Call GetWindowRect(hwndSrc%, winSize)
    nWidth% = winSize.right             ' Units in pixels.

    nHeight% = winSize.bottom           ' Units in pixels.

    ' Assign informate of the destination bitmap.
    hDestDC% = Form1.Picture1.hDC
    x% = 0: Y% = 0

    ' Set global variable TwipsPerPixel and use to set
    ' picture box to same size as screen being grabbed.
    ' If picture box not the same size as picture being
    ' BitBlt'ed to it, it will chop off all that does not
    ' fit in the picture box.
    Form1.Picture1.Top = 0
    Form1.Picture1.Left = 0
    Form1.Picture1.Width = (nWidth% + 1) * TwipsPerPixel
    Form1.Picture1.Height = (nHeight% + 1) * TwipsPerPixel

    ' Assign the value of the constant SRCOPYY to the Raster operation.

    dwRop& = &HCC0020

    ' Note function call must be on one line:
    Suc% = BitBlt(hDestDC%, x%, Y%, nWidth%, nHeight%,
                  hSrcDC%, XSrc%, YSrc%, dwRop&)

    ' Release the DeskTopWindow's hDC to Windows.
    ' Windows may hang if this is not done.
    Dmy% = ReleaseDC(hwndSrc%, hSrcDC%)

    'Make the picture box visible.
    Form1.Picture1.Visible = True
End Sub

Sub GetTwipsPerPixel ()
    ' Set a global variable with the Twips to Pixel ratio.
    Form1.ScaleMode = 3
    NumPix = Form1.ScaleHeight
    Form1.ScaleMode = 1
    TwipsPerPixel = Form1.ScaleHeight / NumPix
End Sub

5. Run the program and click on the form.

6. With the mouse, change the size of the form to see more of the
   picture box. With a little work, you can use this as a "screen
   saver" program.


Author Comment

ID: 1461075
Apparantly it looks good but I will try it for vb5 tonight and let u know tomorror.
in the mean time if u have a vb5 code let me know i will be greatful.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1461076
The only thing you'll need to do to make this work is update the API's to their 32 bit equivilants...

Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC 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 dwRop As Long) As Long

Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Long, ByVal hdc As Long) As Long

Declare Sub GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Long, lpRect As RECT) As Long


Author Comment

ID: 1461077
Yes i had it done thanks a lot any way.
Now the the last part of my question how to save it as a .bmp or .jpg file.
After using the BitBlt function i tried to place it in the clipboard by using the API calls to
but when i use the SetclipboardData i get an error telling me that it cannot find entry point in DLL using "SetclipboardDataA"
SetClipboardData hSrcDC&, vbCFDIB - boom Errorr

btw: after bitblt i cannot access the picture1.picture or picture1.image if i want to shift or move them in a different picturebox or imagebox

and yes i will increase the points if answered

Thanks again and in advance.


Expert Comment

ID: 1461078
Use SavePicture to save the image as a BMP to a specified path, and then just do a LoadPicture on that new BMP. Kind of sneaky, but it works.

Author Comment

ID: 1461079
Acutally it does not work - atleast for me.
After bitblt the image is pasted on the device Context DC.
I tried the following:
SavePicture me.image, "c:\temp\mybmp.bmp"

Listed below is my code see if u can debug for me (thanks)

Sub Load_Form()
hWndSrc& = GetDesktopWindow()
    hSrcDC& = GetDC(hWndSrc&)
    GetWindowRect hWndSrc&, WinSize
    xSrc& = 0
    ySrc& = 0
    nWidth& = WinSize.Right
    nHeight& = WinSize.Bottom
    hDestDC& = Form1.hdc
    xDest& = 0
    yDest& = 0
    dwRop& = &HCC0020
    Tmp& = BitBlt(hDestDC&, xDest&, yDest&, nWidth&, nHeight&, hSrcDC&, xSrc&, ySrc&, dwRop&)
    Tmp& = ReleaseDC(hWndSrc&, hSrcDC&)
End Sub

Author Comment

ID: 1461080
Sorry, I forgot to mention that after applying tbe bitblt and before relleasedc i have the savepicture statement.  It does create bitmap file but blank - all grey color not the actual captured image.

thanks again

Author Comment

ID: 1461081
Try blting into a picture box rather than onto the form.

Author Comment

ID: 1461082
Same result with picturebox also.


Expert Comment

ID: 1461083
Any way i got it done by manually coping the DC to the disk.

thanks a lot

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question