Screen Capture

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Screen Capture

Post 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)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post 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
cecilcheah
Enthusiast
Enthusiast
Posts: 168
Joined: Wed Jun 04, 2003 8:44 am
Location: Switzerland

Post 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
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

And how do you capture an active window at Win 9x then?
darklordz
Enthusiast
Enthusiast
Posts: 119
Joined: Wed May 21, 2003 1:44 pm
Location: Netherlands
Contact:

Post by darklordz »

@dagcrack, detect witch windows os the user has and apply..(if then...)
How would one save the screenshot?
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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")
:)
--Kale

Image
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post 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?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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.
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post by J. Baker »

Thanks for looking that up. :D
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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
--Kale

Image
darklordz
Enthusiast
Enthusiast
Posts: 119
Joined: Wed May 21, 2003 1:44 pm
Location: Netherlands
Contact:

Post by darklordz »

Greate Job!
Image
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post by J. Baker »

Capturing by window name, nice work. :D
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post 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...?
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

I wanted to be able to grab the contents of a window resp. control,
What is window resp. control?
Post Reply