Link to home
Start Free TrialLog in
Avatar of PMH4514
PMH4514

asked on

GetBitmapBits - the iSize paramater.

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:

BYTE* m_pImage;
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?

Thanks!
-Paul
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

A practical approach to reserve buffer space is to assign iSize the lenght of original file.
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PMH4514
PMH4514

ASKER

>> Maximum pixel size is 4 bytes. Allocate 4*width*height pixels and this will be always enough for any bitmap.

That sounds reasonable to me.

Thanks
-Paul