tags:

views:

53

answers:

2

I'm working on writing some automation code and I'm running into a problem finding an efficient way to examine the pixels that presently exist on the display.

I've familiarized myself with the GetPixel function that exists in the gdi32 library, but it's too slow (in fact it takes about 10 seconds just to scan a line of about 1900 pixels).

I've kicked the idea around of making a bitmap out of segments of the graphics device context and testing if this is more efficient (which I can't imagine it would be, considering it would take a lot of r/w to stay reasonably current with the screen image), but before I do that I'd like to know if anyone has any better ideas.

What I'd really love is an efficient way to gather a 2 dimensional array of pixel colors in some or all of the present (or reasonably close to present) graphics context, hopefully using .NET.

+1  A: 

You have to lock the image and work with the memory directly. There is a good article about how to do this here.

Arkain
Thanks. I will give that a shot when I get home from work.
hypoxide
A: 

What I ended up doing was capturing a screenshot of the entire graphics context and searching the resulting bitmap for exactly what I was looking for in a static context. Then, because the area of the graphics output I was concerned with was only about 70x70 pixels, I just captured this small portion every 10 milliseconds and examined it for interesting changes.

This didn't require any unsafe code or locking of the graphics context and was reasonably efficient.

hypoxide