Get/Set the Color of A Pixel

Q:  How can I retrieve or specify the color of specific pixel?

A:  You can use the GetDIBPixelColor function from BiDIB.dll to determine the color of the pixel. This function supports monochrome, 8 and 24 bit per pixel DIBs. You must specify the device independent bitmap, the coordinates of the pixel, and then the function retrieves the color of the specified pixel in the third parameter. You can obtain the handle to the DIB by loading from files, databases etc.

 

      You can specify the color of the pixel using the SetDIBPixelColor function. This function is available in BiDIB.dll. The SetDIBPixelColor works for only monochrome 8 and 24 bit per pixel device independent bitmaps. You must specify the handle of the DIB, the coordinates of the pixel and the new color of the pixel.

Code example:

     #include “BiDib.h”

     …

     HANDLE hDib = LoadDIBFromFile(szFileName);

     if (hDib)

     {

            COLORREF col;

            DWORD x, y;

            …

            If (GetDIBPixelColor(hDib, x, y, &col)

            {// Get the color of the pixel

                        …

            }

            …

            if (SetDIBPixelColor(hDib, x, y, col)

            {// Set the color of the pixel

                        …

            }

     }