Advertisement

How can I get the size of a bitmap?

Started by July 13, 2001 02:34 PM
1 comment, last by Alload 23 years, 7 months ago
Hello, I have to get the width and height size of a bitmap. Do you have an idea of how I could do that? I tried the fonction GetBitmapDimensionEx() but it doesn''t work. I know that the width and height are stored in the bitmap''s header, but how can I access them? Thanks.
Easiest 2 ways I can think of:

1. Load the bitmap with LoadBitmap()/LoadImage() and get information about it with GetObject() (Or another similar function).

2. Open the file yourself and read the header (That''s what you suggested ). Here''s some code for doing that:

  // I''m using fopen() to open the file, you can use C++ streams or whatever if you wantFILE *hFile = fopen(<filename>,"rb");BITMAPINFOHEADER info;fseek(hFile,sizeof(BITMAPFILEHEADER),0); // skip the header, it''s uselessfread(&info,sizeof(BITMAPINFOHEADER),1,hFile); // read the info headerfclose(hFile); // close the file again  


Now the BITMAPINFOHEADER structure has the info you want.
You can also read the header (Instead of skipping it) to make sure it''s really a bitmap:

BITMAPFILEHEADER fHeader;
// read the header here
if (fHeader.bfType != 0x4D42)
{
// not a bitmap...
}


- Goblineye Entertainment
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
Advertisement
Yes, read in the info header to find out the dimensions of the bitmap.

This topic is closed to new replies.

Advertisement