Page 1 of 3

Screen Capture

Posted: Wed Jan 30, 2002 4:39 am
by BackupUser
Code updated for 5.20+

Restored from previous forum. Originally posted by wayne1.

Code: Select all

Procedure CaptureScreen(Left, Top, Width, Height)
  dm.DEVMODE ;structure for CreateDC()
  srcDC = CreateDC_("DISPLAY", "", "", dm)
  trgDC = CreateCompatibleDC_(srcDC)
  BMPHandle = CreateCompatibleBitmap_(srcDC, Width, Height)
  SelectObject_( trgDC, BMPHandle)
  BitBlt_( trgDC, 0, 0, Width, Height, srcDC, Left, Top, #SRCCOPY)
  OpenClipboard_(#Null)
  EmptyClipboard_()
  SetClipboardData_(2, BMPHandle)
  CloseClipboard_()
  DeleteDC_( trgDC)
  ReleaseDC_( BMPHandle, srcDC)
  ProcedureReturn
EndProcedure

CaptureScreen( 0, 0, 800, 600);set to desired(current) screen resolution 1024*768 640*480 etc.

MessageRequester("Message","OK, paste the current clipboard data to Microsoft Paint or whatever program you use to see the results of the screen capture.",#MB_ICONEXCLAMATION)

Posted: Wed Jan 30, 2002 12:33 pm
by BackupUser
Restored from previous forum. Originally posted by PB.

Wayne's tip is useful for grabbing part of a screen, but if you're only interested
in the full screen or current active window, then it can be done shorter like so:

Code: Select all

keybd_event_(#VK_SNAPSHOT,0,0,0) ; Snapshot of entire desktop.
keybd_event_(#VK_SNAPSHOT,1,0,0) ; Snapshot of current active window.

Note that the snapshot is stored in the clipboard like Wayne's example.


PB - Registered PureBasic Coder

Edited by - PB on 30 January 2002 12:34:34

Posted: Sun Jun 15, 2003 8:37 pm
by cecilcheah
Hi there

Code: Select all

keybd_event_(#VK_SNAPSHOT,0,0,0) ; Snapshot of entire desktop.
looks like it does not work for win 95/98, it needs to be:

Code: Select all

keybd_event_(#VK_SNAPSHOT,1,0,0) ; Snapshot of entire desktop.
Anyone knows what about Win NT, Win 2000 or Win XP?

Cecil

Posted: Sun Jun 15, 2003 9:13 pm
by PB
You're correct, Cecil -- Win 9x needs 1,0,0 for the entire desktop.
On Windows 2000 you use 0,0,0 for desktop and 1,0,0 for window.

Posted: Mon Mar 08, 2004 2:38 am
by dagcrack
And how do you capture an active window at Win 9x then?

Posted: Sat Mar 13, 2004 5:41 pm
by darklordz
@dagcrack, detect witch windows os the user has and apply..(if then...)
How would one save the screenshot?

Posted: Sat Mar 13, 2004 8:11 pm
by Kale

Code: Select all

;capture a piece of screen
Procedure.l CaptureScreen(Left.l, Top.l, Width.l, Height.l)
    dm.DEVMODE
    BMPHandle.l
    srcDC = CreateDC_("DISPLAY", "", "", dm)
    trgDC = CreateCompatibleDC_(srcDC)
    BMPHandle = CreateCompatibleBitmap_(srcDC, Width, Height)
    SelectObject_( trgDC, BMPHandle)
    BitBlt_( trgDC, 0, 0, Width, Height, srcDC, Left, Top, #SRCCOPY)
    DeleteDC_( trgDC)
    ReleaseDC_( BMPHandle, srcDC)
    ProcedureReturn BMPHandle
EndProcedure

ScreenCaptureAddress = CaptureScreen(192, 112, 256, 256)

CreateImage(0, 256, 256)
StartDrawing(ImageOutput())
    DrawImage(ScreenCaptureAddress, 0, 0)
StopDrawing()
SaveImage(0, "Screenshot.bmp")
:)

Posted: Sun Mar 14, 2004 5:28 am
by J. Baker
I was trying to figure out how to capture the screen of a window, no matter what the position of the window is. I understand most of the procedure but what does the "DC" stand for?

Posted: Sun Mar 14, 2004 6:47 am
by Dare2
Good question.

I looked it up, and according to the sdk it is "Device Context".
A device context is a structure that defines a set of graphic objects and their associated attributes, as well as the graphic modes that affect output. The graphic objects include a pen for line drawing, a brush for painting and filling, a bitmap for copying or scrolling parts of the screen, a palette for defining the set of available colors, a region for clipping and other operations, and a path for painting and drawing operations.

Posted: Sun Mar 14, 2004 7:46 am
by J. Baker
Thanks for looking that up. :D

Posted: Sun Mar 14, 2004 2:39 pm
by Kale
I was trying to figure out how to capture the screen of a window, no matter what the position of the window is

Code: Select all

;capture a piece of screen 
Procedure.l CaptureScreen(Left.l, Top.l, Width.l, Height.l) 
    dm.DEVMODE 
    BMPHandle.l 
    srcDC = CreateDC_("DISPLAY", "", "", dm) 
    trgDC = CreateCompatibleDC_(srcDC) 
    BMPHandle = CreateCompatibleBitmap_(srcDC, Width, Height) 
    SelectObject_( trgDC, BMPHandle) 
    BitBlt_( trgDC, 0, 0, Width, Height, srcDC, Left, Top, #SRCCOPY) 
    DeleteDC_( trgDC) 
    ReleaseDC_( BMPHandle, srcDC) 
    ProcedureReturn BMPHandle 
EndProcedure 

Procedure CaptureWindow(WindowName.s)
    WinHndl = FindWindow_(0, WindowName)
    If WinHndl
        WindowSize.RECT
        GetWindowRect_(WinHndl, @WindowSize)
        ScreenCaptureAddress = CaptureScreen(WindowSize\Left, WindowSize\Top, WindowSize\Right - WindowSize\Left, WindowSize\Bottom - WindowSize\Top) 
        CreateImage(0, WindowSize\Right - WindowSize\Left, WindowSize\Bottom - WindowSize\Top)
        StartDrawing(ImageOutput()) 
            DrawImage(ScreenCaptureAddress, 0, 0) 
        StopDrawing() 
        SaveImage(0, "Screenshot.bmp")
    Else
        ProcedureReturn 0
    EndIf
    ProcedureReturn 1
EndProcedure

CaptureWindow("My Computer")
:D

Posted: Sun Mar 14, 2004 11:08 pm
by darklordz
Greate Job!

Posted: Mon Mar 15, 2004 12:47 am
by J. Baker
Capturing by window name, nice work. :D

Posted: Mon Mar 15, 2004 9:27 pm
by dell_jockey
o.k. Group, very nice work indeed.

And now for the next step... :D

What, if I wanted to be able to grab the contents of a window resp. control, but I'd like to use the resulting bitmap for publishing purposes. For the resulting bitmap, I'd need higher resolutions than the screen grab can deliver. Also, to be able to publish high quality pictures, I don't want to scale up an original screen resolution screen grab....

Ideas anyone...?

Posted: Mon Mar 15, 2004 10:30 pm
by einander
I wanted to be able to grab the contents of a window resp. control,
What is window resp. control?