Given an HBITMAP I want to extract the bitmap data in 24-bit RGB format
regardless of the number of bits per pixel of the original.
This doesn't work below but am I on the right lines...?
void CreateFromHandle(HBITMAP hBitmap)
{
// Extract bitmap data from HBITMAP or MFC CBitmap.
HDC hDC = CreateCompatibleDC(NULL);
BITMAP bm;
// Get the dimensions of the bitmap from the handle
assert(GetObject(hBitmap, sizeof(BITMAP), &bm) == sizeof(BITMAP));
// Allocate a 24-bit buffer to receive the bitmap bits.
// Don't need to allocate a color table as the data will be
requested in 24-bits??
DWORD dwCount = (DWORD) ((bm.bmWidth * bm.bmHeight) + 1) * 3;
void *pBits = new BYTE[dwCount];
// Initialise the first 6 members of BITMAPINFOHEADER
// as specified by the DEVSTUDIO help page.
BITMAPINFO bmInfo;
bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biWidth = bm.bmWidth;
bmInfo.bmiHeader.biHeight = bm.bmHeight;
// Does GetDIBits convert like this???
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biBitCount = 24;
bmInfo.bmiHeader.biCompression = BI_RGB;
// Get the bitmap data.
int numOfScanLines = GetDIBits(hDC, hBitmap, 0, bm.bmHeight, pBits,
&bmInfo, DIB_RGB_COLORS);
assert(numOfScanLines == bm.bmHeight);
// Data is now in RGB values???
// Dispose of buffer.
delete[] pBits;
DeleteDC(hDC);
}
Stu.
>Hi,
>
>Given an HBITMAP I want to extract the bitmap data in 24-bit RGB format
>regardless of the number of bits per pixel of the original.
>
>This doesn't work below but am I on the right lines...?
>
>void CreateFromHandle(HBITMAP hBitmap)
>{
> // Extract bitmap data from HBITMAP or MFC CBitmap.
> HDC hDC = CreateCompatibleDC(NULL);
> BITMAP bm;
>
> // Get the dimensions of the bitmap from the handle
> assert(GetObject(hBitmap, sizeof(BITMAP), &bm) == sizeof(BITMAP));
>
> // Allocate a 24-bit buffer to receive the bitmap bits.
> // Don't need to allocate a color table as the data will be
>requested in 24-bits??
I think you'll find that instead of the colour table, there are three
'mask' DWORDs, which specify which bits of the 24 represent red, green
and blue. (Don't get excited - only certain values are legal!) There's
a KnowledgeBase article on this somewhere in the DevStudio CDRom.
> DWORD dwCount = (DWORD) ((bm.bmWidth * bm.bmHeight) + 1) * 3;
Try:
bm.bmWidth * 3 * bm.bmHeight * sizeof (DWORD)
> void *pBits = new BYTE[dwCount];
>
> // Initialise the first 6 members of BITMAPINFOHEADER
> // as specified by the DEVSTUDIO help page.
> BITMAPINFO bmInfo;
>
> bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
> bmInfo.bmiHeader.biWidth = bm.bmWidth;
> bmInfo.bmiHeader.biHeight = bm.bmHeight;
>
> // Does GetDIBits convert like this???
> bmInfo.bmiHeader.biPlanes = 1;
> bmInfo.bmiHeader.biBitCount = 24;
>
> bmInfo.bmiHeader.biCompression = BI_RGB;
>
> // Get the bitmap data.
> int numOfScanLines = GetDIBits(hDC, hBitmap, 0, bm.bmHeight, pBits,
>&bmInfo, DIB_RGB_COLORS);
> assert(numOfScanLines == bm.bmHeight);
>
> // Data is now in RGB values???
>
Yes. In the buffer. So don't
> // Dispose of buffer.
! <g>
> delete[] pBits;
>
> DeleteDC(hDC);
>}
>
>Stu.
>
Pete Barrett