views:

190

answers:

1

I was trying some code to capture part of the screen using getPixel on Windows, with the device context being null (to capture screen instead of window), but it was really slow. It seems that GetDIBits() can be fast, but it seems a bit complcated... I wonder if there is a library that can put the whole region into an array, and pixel[x][y] will return the 24 bit color code of the pixel?

Or does such library exist on the Mac? Or if Ruby or Python already has such a library that can do that?

+2  A: 

I've never done this but I'd try to:

  1. Create a memory Device Context (DC) using CreateCompatibleDC passing it the Device Context of the desktop (GetDC(NULL)).

  2. Create a Bitmap (using CreateCompatibleBitmap) the same size as the region your capturing.

  3. Select the bitmap into the DC you created (using SelectObject).

  4. Do a BitBlt from the desktop DC to the DC you created (using SRCCOPY flag).

Working with Device Contexts can cause GDI leaks if you do things in the wrong order so make sure that you read the documentation on all the GDI functions you use (e.g. SelectObject, GetDC, etc.).

Tim Stewart