Finding the image dimensions of a file without reading it

Posted on 2004-11-16
Last Modified: 2010-04-24

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 ;


// 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 ;

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.


Question by:jeffreyg
    LVL 48

    Accepted Solution

    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.

    Author Comment

    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.



    Author Comment

    P.S. I want to load all image types esp. jpg, bmp, tiff, gif.



    Author Comment

    Thanx Alex,

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now