2007-01-17

Capture Screens with GdiPlus-X

To capture a screen with Gdiplus-X is a very easy task too.
basically, all we need to do is to call the fromscreen() method from the bitmap class. To ease this task, this method brings some different overloads.

 

important:

All samples below use the Gdiplus-x library from VFPX. Download the latest stable release from codeplex:
http://www.codeplex.com/wiki/view.aspx?projectname=vfpx&title=gdiplusx

 

1 - capture a form screen sending the hwnd of the form or the form as an object 
do locfile("system.app") 

local locapturebmp as xfcbitmap
with _screen.system.drawing
locapturebmp = .bitmap.fromscreen(thisform.hwnd)
* could be also:
* locapturebmp = _screen.system.drawing.bitmap.fromscreen(thisform)
locapturebmp.save("c:\captured.png", .imaging.imageformat.png)
endwith

 2 - capture the whole screen
in this case, no parameter is needed to be passed
do locfile("system.app")

local locapturebmp as xfcbitmap
with _screen.system.drawing
locapturebmp = .bitmap.fromscreen()
locapturebmp.save("c:\capturedscreen.png", .imaging.imageformat.png)
endwith

 

3 - capture the screen of a form cutting off its borders and title.
for this task we use the sysmetric() function to obtain the measure of the screen elements, such as the title height, top and left borders. then we use another overload, sending the hwnd, and the coordinates of the form to be captured.

 
do locfile("system.app")

local lntitleheight, lnleftborder, lntopborder
lntitleheight = sysmetric(9)
lnleftborder = sysmetric(3)
lntopborder = sysmetric(4)

local locapturebmp as xfcbitmap

with _screen.system.drawing
locapturebmp = .bitmap.fromscreen(;
thisform.hwnd, ;
lnleftborder, ;
lntitleheight + lntopborder, ;
thisform.width, ;
thisform.height)

locapturebmp.save("c:\captured.png", .imaging.imageformat.png)
endwith




 

4 - capture all forms in the screen.
this is very simple too. just create a loop through all _screen forms and capture each of them, sending the form.hwnd as a parameter.

 
do locfile("system.app")

local locapturebmp as xfcbitmap
local n
local loform as form
n = 1

with _screen.system.drawing

for each loform in _screen.forms
locapturebmp = .bitmap.fromscreen(loform.hwnd)
locapturebmp.save("c:\capturedform" + transform(n) + ".png", ;
.imaging.imageformat.png)
n = n + 1
endfor

endwith

4 comments:

  1. Este articulo esta traducido al español en www.PortalFox.com


    "Capturando pantallas con GdiPlus-X"

    http://www.portalfox.com/article.php?sid=2346

    ReplyDelete
  2. Hey, Cesar. How would you capture a BMP if you wanted to be able to tweak the alpha? Thanks.
    Hi Garret,
    What exactly do you mean here ? Can you send a sample showing what you need here ?
    Regards
    Cesar

    ReplyDelete
  3. Thank you and i just used it in replacement of my DDE general function.


    Regards,


    gino

    Saudi Arabia

    ReplyDelete
  4. Hey Cesar...


    First of all thanks for the samples and great help to all...


    Now the question, it's possible to take a picture of a not visible control or outside the windows desktop?


    Thanks

    ReplyDelete