How to determine dimensions of a bitmap?

Started by
4 comments, last by Buiden 20 years ago
Hey this might seem like a really stupid question, since I have only been at DirectX for a couple months now. I have been using the D3DXCreateTextureFromFileEx function to create textures from .bmp files. For the width/height parameters I pass D3DX_DEFAULT and it pulls the width/height from the file. Well I am wondering what the fastest/best way to pull the width/height of the bitmap from a file for my own use. I know that it can be done using windows GDI but I am wondering if there is a less painful method of doing so. Thanks, _Buiden
Advertisement
Read the file as binary data. The first chunk of the file should correspond with a BITMAPFILEHEADER structure, after the BITMAPFILEHEADER should be a BITMAPINFOHEADER. The BITMAPINFOHEADER has the height and width as data members. You should have access to both struct definitions if you include windows.h.
Sounds good to me, thanks

_Buiden
Or, you could just open the file up and read in 2 long values (shorts work as well).

long Width,Height;
FILE *in = fopen(BitmapFileName,"rb");
fseek(in,18,0); //Seek to position 18, from start of file!
fread(&Width,4,1,in);
fread(&Height,4,1,in);
fclose(in);

Or with shorts:
short Width,Height;
FILE *in = fopen(BitmapFileName,"rb");
fseek(in,18,0); //Seek to position 18, from start of file!
fread(&Width,2,1,in);
fseek(in,22,0); //Seek to position 22, from start of file!
fread(&Height,2,1,in);
fclose(in);

Nice and easy, and it only requires reading in 4-8 bytes of data .
D3DXGetImageInfoFromFile is even easier. And it will automatically work with any file type that D3DX can load.
Stay Casual,KenDrunken Hyena
Thanks DH, thats more along the lines of what I was looking for

_Buiden

This topic is closed to new replies.

Advertisement