0

How would I replace GetPixel() with something faster?

Currently I am using:

temp = GetPixel(hMonitor, 1, 1);
if (pixelArray[0] != temp)
{
    pixelArray[0] = temp;
    counter++;
}

Above code is just a simplified example.

This is contained in a for loop for all the pixels on the display. It compares one pixel (temp) against the previous array's pixel (pixelArray). If it has changed, then replace it. How-ever I am finding that using GetPixel() for every pixel on the display takes a long time.

I have been reading other questions of a similar nature such as:

Fastest method of screen capturing

Get Pixel color fastest way?

...but I am not sure which method is better such as GDI or DirectX nor how I would implement said methods.

Update: Windows GDI (using GetObject) to an array of the pixels is what I needed, thank you. This is much, much faster than GetPixel().

Community
  • 1
  • 1
Built on Sin
  • 351
  • 2
  • 6
  • 19
  • pencil, paper, and a magnifying glass is more efficient than `GetPixel` so take your pick. – Captain Obvlious Nov 04 '14 at 03:36
  • How would I implement that into my current code? – Built on Sin Nov 04 '14 at 03:39
  • What is the problem you are actually trying to solve with this? – Chuck Walbourn Nov 04 '14 at 05:39
  • @ChuckWalbourn Essentially to keep a screenshot in memory or disk per monitor, then every minute or so check to see if there has been any change (If their has increase counter and replace byte, unsigned long, etc.) – Built on Sin Nov 04 '14 at 13:04
  • The problem is not that you can't find a solution, you already have found from the links you posted, but how to implement these. Meaning you don't understand them. You need to learn GDI or directX first. This is how I did it and this is how you should do it too. – γηράσκω δ' αεί πολλά διδασκόμε Nov 04 '14 at 16:50
  • You are still describing a solution, not the problem. What is it you are actually trying to do? – Chuck Walbourn Nov 04 '14 at 18:31
  • @ChuckWalbourn The problem is using the method GetPixel to create a screenshot or even a few pixels in an array is far too slow for the task I am trying to do. I guess I should have used the word slow, rather than inefficient. For all pixels displayed on monitor, create array / compare to previous array. – Built on Sin Feb 27 '16 at 00:25

1 Answers1

1

I would suggest you retrieve a pointer to the bitmap's pixel data (assuming you have a HBITMAP handle).

This is done via GetObject(), which should return you a BITMAP structure. This is the field you are interested in:

bmBits: A pointer to the location of the bit values for the bitmap. The bmBits member must be a pointer to an array of character (1-byte) values.

Then you can run your checking logic per pixel on the buffers. That would be way faster from using GetPixel.

Anton Angelov
  • 1,223
  • 7
  • 19