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
  • 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.
Technology Partners: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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