Solved

Capture Screen Image???

Posted on 1998-04-12
11
663 Views
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.
Thanks.
0
Comment
Question by:msali
  • 6
  • 4
11 Comments
 
LVL 2

Expert Comment

by:woka
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.
0
 
LVL 2

Accepted Solution

by:
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

Summary:

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:

Example
-------

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:

Global.Bas
----------

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

Form1
-----

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.
    GetTwipsPerPixel
    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.


0
 

Author Comment

by:msali
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.
thanks.
msa
0
 
LVL 2

Expert Comment

by:SGC
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

0
 

Author Comment

by:msali
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
OpenClipboard
EmptyClipboard
SetClipboardData
CloseClipboard
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.
msali

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:SGC
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.
0
 

Author Comment

by:msali
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
   
    GetTwipsPerPixel
   
    dwRop& = &HCC0020
   
    Tmp& = BitBlt(hDestDC&, xDest&, yDest&, nWidth&, nHeight&, hSrcDC&, xSrc&, ySrc&, dwRop&)
    Tmp& = ReleaseDC(hWndSrc&, hSrcDC&)
   
End Sub
0
 

Author Comment

by:msali
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
0
 

Author Comment

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

Author Comment

by:msali
ID: 1461082
Same result with picturebox also.

thanks
0
 
LVL 2

Expert Comment

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

thanks a lot
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now