GetDIBits and HBITMAP

Hi,

I'm attempting to get the RGB data from a HBITMAP, but I'm struggling to piece it together using GetDIBits, would somebody be kind enough to show me how to use GetDIBits to get pixel data from a HBITMAP and then retrieve individual pixel values from GetDIBits?

Thank you,
Uni
LVL 3
Unimatrix_001Asked:
Who is Participating?
 
JohnGabyCommented:
Where does your HBITMAP come from? If the bitmap is a device independent bitmap, then HPALETTE can be null.

The pixel data you get back depends on what format you ask for.  In the example I gave you, I asked for 'PixelFormat24bppRGB' which returns a 24 bit bitmap.  Thus the pointer pPixelData points to the first scan line, and the first pixel is represented by the first three bytes (either RGB or GBR, I don't remember which).  The next pixel is represented by the next 3 bytes, and so on.  An entire scan line is data.Stride bytes long (NOT width * 3).  So if you wanted to look at each pixel, you might do something like the attached code snippet

    unsigned char * p;
    int             x;
    int             y;
 
    for (y = 0 ; y < data.Height ; y++)
    {
        p   = ((unsigned char *) data.Scan0) + (y * data.Stride);
 
        for (x = 0 ; x < data.Width ; x++, p += 3)
        {
            int red     = p[0];     // Note, that I might have these backwards
            int green   = p[1];
            int blue    = p[2];
        }
    }

Open in new window

0
 
JohnGabyCommented:
If you use GDI+, you can access the RGB data very easily, the following code snippet shows you how.  Info for the relevant functions can be found at:

http://msdn.microsoft.com/en-us/library/ms536314(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms536297(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms536298(VS.85).aspx


void ReadBitmap(HBITMAP hBitmap, HPALETTE hPal)
{
    Bitmap	myBitmap(hBitmap, hPal);
 
    /*
     * To get an individual pixel
     */
    Color color;
    if (myBitmap.GetPixel(0, 0, &color) == Ok)
    {
        int red   = color.Red;
        int blue  = color.Blue;
        int green = color.Green;
    }
 
    /*
     * To get the entire bitmap
     */
    BitmapData	data;
    Rect		rect(0, 0, myBitmap.GetWidth(), myBitmap.GetHeight());
    if (myBitmap.LockBits(&rect, ImageLockModeRead, PixelFormat24bppRGB, &data))
    {
        LPUBYTE	pPixelData = (LPUBYTE) data.Scan0;
 
        myBitmap.UnlockBits(&data);
    }
}

Open in new window

0
 
Unimatrix_001Author Commented:
Hi John,

I'll probably use the second one which fetches the entire bitmap, but could you give a quick simple example on how to use the second method to access the pixel values within pPixelData? Also, what would I pass into HPALETTE as I don't do anything specific with a palette?

Thank you,
Uni
0
 
Unimatrix_001Author Commented:
That seems reasonable. Thank you. :)
0
 
Unimatrix_001Author Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.