Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Finding the image dimensions of a file without reading it

Posted on 2004-11-16
4
Medium Priority
?
169 Views
Last Modified: 2010-04-24
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.
0
Comment
Question by:jeffreyg
  • 3
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 750 total points
ID: 12594336
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
 

Author Comment

by:jeffreyg
ID: 12594778
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
 

Author Comment

by:jeffreyg
ID: 12594789
P.S. I want to load all image types esp. jpg, bmp, tiff, gif.

regards,

Gordon.
0
 

Author Comment

by:jeffreyg
ID: 12665867
Thanx Alex,

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

:))
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

804 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