Is there a faster alternative to GDI GetPixel ()?

I am using GetPixel() from gdi32.dll in a .NET application to try pixel color anywhere on the screen. It works fine, but this is a serious bottleneck for me.

Is there a faster way to do this?

+7
source share
2 answers

Quick access to pixels is possible using the LockBits() method for Bitmap . This will return an object containing a pointer to the start of the pixel data, and you can use unsafe code to access memory.

http://www.bobpowell.net/lockingbits.htm

+8
source

GetPixel slow for two reasons:

  • Since you are viewing the screen, each call to GetPixel results in a transaction to the video driver, which in turn takes the pixel data from the video memory.

    In constrast mode, using GetPixel on the DIB is much faster.

  • In any case, GetPixel performs several actions, including cropping / converting coordinates, etc.

So, if you use to request many pixel values โ€‹โ€‹at the same time, you should try to organize this in one transaction with the GDI / video driver.

Using GDI, you must create a DIB of the appropriate size (see CreateDIBSection ). Once created, you will be given a direct pointer to the image bit data. Then copy part of the image to your DIB (see BitBlt ). Also remember to call GdiFlush before you actually check the contents of the DIB (as video drivers can do asynchronous drawing).

Using GD +, you can do the same with a slightly simpler syntax.

+5
source

Source: https://habr.com/ru/post/911668/


All Articles