[Last Call] Learn how to a build a cloud-first strategyRegister Now


Capture Screen Image???

Posted on 1998-04-12
Medium Priority
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 160 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.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

825 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