How to determine dimensions of a bitmap?
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
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.
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 .
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement