Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting RGB values from HBITMAP

937 views
Skip to first unread message

Thomas Johnson

unread,
Apr 8, 1999, 3:00:00 AM4/8/99
to
How can I get the RGB values for each pixel in an HBITMAP!

Please don't send me the name of a function, if you can help please
send a tidbit of code to use!

-Thanks


Lee Kamentsky

unread,
Apr 8, 1999, 3:00:00 AM4/8/99
to
Well, Mr. Johnson, here's the tidbit, at least for MFC:
CDC dcMemory;
dcMemory.CreateCompatibleDC(NULL);
dcMemory.SaveDC();
dcMemory.SelectObject(pDoc->m_bitmap);
COLORREF color=dcMemory.GetPixel(point);
char buffer[200];
sprintf(buffer, "Red: %d, Green: %d, Blue: %d", GetRValue(color),
GetGValue(color), GetBValue(color));
AfxGetMainWnd()->SetWindowText(buffer);

There are faster ways than examining each pixel using GetPixel. My personal
choice would be to create a true-color (24 bit) section DIB. I'd select that
bitmap into one memory DC and the bitmap to be examined into another memory
DC. I'd blit the bitmap to be examined onto the section DIB, then I'd
examine the section DIB's memory; the colors would be neatly laid out: red
byte, green byte, blue byte, red byte, green byte, blue byte.

I have a whole MFC app that allows you to load a bitmap and examine each of
its pixels. I can E-Mail it to you on request.

--Lee
Thomas Johnson <por...@idcomm.com> wrote in message
news:370D5CF8...@idcomm.com...

Tony B.

unread,
Apr 9, 1999, 3:00:00 AM4/9/99
to
Thomas Johnson wrote in message <370D5CF8...@idcomm.com>...

> How can I get the RGB values for each pixel in an HBITMAP!
>
> Please don't send me the name of a function, if you can help please
>send a tidbit of code to use!
>
>-Thanks
>

HDC hdc;
COLORREF col;
DWORD red,green,blue;
HBITMAP bitmap,oldbitmap;

// Assuming you already have the HBITMAP setup (no error checking)

// Create a device context same as the screen
hdc = CreateCompatibleDC(NULL);

// Select the HBITMAP into that device context
oldbitmap = (HBITMAP)SelectObject(hdc,bitmap);

// Get the color and RGB values
col=GetPixel(hdc,x,y);
red=GetRValue(col);
green=GetGValue(col);
blue=GetBValue(col);

// Clean up
SelectObject(hdc,oldbitmap);
DeleteDC(hdc);


0 new messages