Search code examples
c++windowsdirectxgdi

Faster than GetPixel()?


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().


Solution

  • 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.