Finding the image dimensions of a file without reading it

Guys,

I am wanting to find the dimensions of a image quickly. Now I know that there is a BITMAPFILEHEADER structure which can allow me to read the portion of the bitmap I want to read to get the width and height of the image.

-----
CFile file;
// sBMPFileName is the BMP filename
if( !file.Open( sBMPFileName, CFile::modeRead) )
  return ;

BITMAPFILEHEADER bmfHeader;

// Read file header
if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
  return ;

// File type should be 'BM'
if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B'))
  return ;

BITMAPINFOHEADER bmiHeader;
if (file.Read((LPSTR)&bmiHeader, sizeof(bmiHeader)) != sizeof(bmiHeader))
  return ;


int bmWidth = bmiHeader.biWidth;
int bmHeight = bmiHeader.biHeight;
------

I want to be able to do this for all images. I have potentially thousands of images and the system need to perform an add of an image size e.g. 6x4" print from the file. I want to make sure that the image is off sufficient quality to allow reasonable printing quality.

Is there something similar to this in mfc / gdi+ to do the same thing for all image types .... jpg, bmp, tiff, pcx.

regards,

Gordon.
jeffreygAsked:
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.

AlexFMCommented:
If you are interesting in all image types supported by GDI+, you can do the following:

BOOL GetImageDimensions(LPWSTR sFileName, int& nWidth, int& nHeight)
{
    Bitmap* pBitmap = Bitmap::FromFile(sFileName, FALSE);

   if ( pBitmap == NULL )
       return FALSE;

    nWidth = pBitmap->GetWidth();
    nHeight = pBitmap->GetHeight();

    delete pBitmap;

    return TRUE;
}

If you need only .bmp files, use GDI LoadImage function with IMAGE_BITMAP parameter and LR_LOADFROMFILE flag. It returns HBITMAP. Call GetObject for this HBITMAP and get BITMAP structure. It contains bmWidth and bmHeight members.
0

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
jeffreygAuthor Commented:
Does this FromFile not load the image into the Bitmap?

I am using that already, for images that I display on the screen (12 thumbnails). I do what you suggest already.

1. Find out how many images there are in the directory I am in, by using CFileFind.
2. Create a linked list of placeholders for all the files.
3. Load the first 12 images in with the Bitmap::FromFile() function.
4. Don't load any more images atm, what if the user wants only the first image in a list of 2000 images?
5. If the user wants to view the next 12 images repeat process 3.
6. If the user clicks the select all images for a 6x4 print, load all the images that fulfill this criteria.

Now if the user does step 6 without loading all the images I don't know the image dimension.

Ideally when create my liked list of placeholders in step 2 I want to get the header information of the each image only.

regards,

Gordon.
0
jeffreygAuthor Commented:
P.S. I want to load all image types esp. jpg, bmp, tiff, gif.

regards,

Gordon.
0
jeffreygAuthor Commented:
Thanx Alex,

Best method I could find. It'll have to do.

:))
0
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
Visual C++.NET

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.