PDA

View Full Version : Help with getting raw pixels from screen (lazarus)



jdarling
15-08-2009, 02:09 AM
Hey all, I've been working on a little side project while I figure things out for my PGD entry. Now though, I'm at a problem. I'm working in Lazarus (windows only) and need to get the raw screen pixel information into a buffer.

Typically in Delphi this is easy. Create a bitmap, size it, Call BitBlt on its HDC, and then copy the scanline[0] into your working array. In lazarus of course we don't have scanline property :(.

I thought that just simply creating the memory block and calling BitBlt passing the block might work, but no go. Code is below (doesn't work)


function ScreenCapRectPtr(R: TRect) : pointer;
var
sdc : HDC;
begin
result := GetMem((R.Right - R.Left)*(R.Bottom- R.Top));
sdc := GetDC(0);
try
BitBlt(HDC(result), 0, 0, (R.Right - R.Left), (R.Bottom- R.Top), sdc, R.Left, R.Top, SRCCOPY);
finally
ReleaseDC(0, sdc);
end;
end;


Anyone know where to find info on WHAT exactly an HDC really is, or have code that works in Lazarus that achieves the desired result?

Thanks,
- Jeremy

paul_nicholls
15-08-2009, 11:06 AM
Perhaps this link will help:

http://wiki.lazarus.freepascal.org/Developing_with_Graphics

Specifically this info:

Taking a screenshot of the screen

Since Lazarus 0.9.16 you can use LCL to take screenshots of the screen on a cross-platform way. The following example code does it (works on gtk2 and win32, but not gtk1 currently):


uses LCLIntf, LCLType;

...

var
MyBitmap: TBitmap;
ScreenDC: HDC;
begin
MyBitmap := TBitmap.Create;
ScreenDC := GetDC(0);
MyBitmap.LoadFromDevice(ScreenDC);
ReleaseDC(ScreenDC);


cheers,
Paul

jdarling
16-08-2009, 03:57 PM
Actually, yes, I have read that as well as many other articles on the subject. Unfortunately it has little to do with the price of tea in china ;). This has only to do with getting the pixel data from an HDC (desktop is just a good starting point, in the end its a remote HDC that will be processed).

But, I did find that (finally) the Lazarus team has surfaced TBitmap.RawImage.Data (YEAH!) and this gives me direct access to the pixel data held within the HDC once I do a BitBlt to the surface. So, alls good now, just access that as though I were accessing Scanline :).