GetBitmapBits - the iSize paramater.
Posted on 2004-11-30
This might sound silly, perhaps it is.
If I load an image:
m_hbmImage = (HBITMAP) ::LoadImage( NULL, strImageName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
I want to use GetBitmapBits to pull the pixel data into a BYTE array:
m_pImage = new BYTE[iSize];
But the problem is, I need to first determine iSize, which means I first need to know the number of bytes in the image.
I realize that GetBitmapBits returns the actual number retrieved, MSDN says:
The dwCount parameter specifies the number of bytes to be copied to the buffer. Use CGdiObject::GetObject to determine the correct dwCount value for the given bitmap.
And for GetObject, MSDN states:
If the object is a CBitmap object, GetObject returns only the width, height, and color format information of the bitmap. The actual bits can be retrieved by using CBitmap::GetBitmapBits.
So the documentation for GetBitmapBits tells me to look at GetObject to determine the size paramater, and the documentation for GetObject tells me to look at GetBitmapBits for the paramater. Circular?
What I did which works, but I don't know if it's "appropriate" and thus I assume there should be a better way, is do:
BYTE* m_pImage = NULL;
int iCount = ::GetBitmapBits(m_hbmImage, 6000000, m_pImage);
m_pImage = new BYTE[iGetCount];
memset(m_pImage, 0, iGetCount);
iCount = ::GetBitmapBits(m_hbmImage, iGetCount, m_pImage);
So I used 6000000 as an arbitrarilly high number, a number higher than any possible image I might be loading. Called GetBitmapBits with that high value only so that I can determine the actual size. Then I called it again, after having initialized the byte array to the proper size.
It seems too complex and just wrong. What is the appropriate way to load an external image, initialize an appropriate sized array and copy the bits of the image into it?