GetBitmapBits - the iSize paramater.

This might sound silly, perhaps it is.

If I load an image:


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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
A practical approach to reserve buffer space is to assign iSize the lenght of original file.
Jaime OlivaresSoftware ArchitectCommented:
Maximum pixel size is 4 bytes. Allocate 4*width*height pixels and this will be always enough for any bitmap. To know bitmap width and height use GetObject API with m_hbmImage.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PMH4514Author Commented:
>> 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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

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.