How to find height and width of jpg image?

I need to find out the height and width of jpg image in MFC?
If user has selected one jpg image, how can i find out the size of image?
LearningCppAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

ZoppoCommented:
Hi LearningCpp,

you could load the JPG using 'LoadImage', cast the returned handle to be a HBITMAP and call 'GetObject' to fill a BITMAP struct - this contains width and height of the image ...

Hope that helps,

ZOPPO
0
AndyAinscowFreelance programmer / ConsultantCommented:
Something like this should be fine.

CImage img;
img.Load("file path goes here ");
int w = img.GetWidth();
int h = img.GetHeight();
0
waysideCommented:
You can also parse out the info from the header, the format is pretty simple.

A good description of the header is here:

http://www.obrador.com/essentialjpeg/headerinfo.htm
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

waysideCommented:
I was bored, so I write some code:


#include <stdio.h>
 
bool get_image_size(int &x, int& y, const char *filename)
{
  FILE *fp = fopen(filename, "rb");
  if (fp == NULL)
    return false;
    
  // make sure its a jpg
  unsigned int b1=0, b2=0;
  b1 = fgetc(fp);
  b2 = fgetc(fp);
  if (b1 != 0xFF || b2 != 0xD8)
  {
    fclose(fp);
    return false;
  }
    
  // read until we find the first frame marker - 0xFFC0
  b1 = 0;
  b2 = 0;
  while (1) 
  {
    b1 = fgetc(fp);
    if (b1 == 0xFF) 
    {
      b2 = fgetc(fp);
      if (b2 == 0xC0) 
      {
        break; // found it:
      }
    }
    if (b1 == EOF)
      break;
  }
  
  if (b1 != 0xFF || b2 != 0xC0)
  {
    fclose(fp);
    return false;
  }
    
  b1 = fgetc(fp);
  b2 = fgetc(fp); //length
  b1 = fgetc(fp); // precision
  b1 = fgetc(fp);
  b2 = fgetc(fp); // y size
  
  y = (b1 << 8) + b2;
  
  b1 = fgetc(fp);
  b2 = fgetc(fp); // x size
  
  x = (b1 << 8) + b2;
  
  fclose(fp);
  return true;
  
}
 
int main(int argc, char ** argv) 
{
  if (argc < 2) 
  {
    printf("%s <file.jpg>\n", argv[0]);
    return 0;
  }
  
  int x, y;
  
  bool result = get_image_size(x, y, argv[1]);
  if (result) {
    printf("size is %d x %d\n", x, y);
  }
  else
  {
    printf("File not found or error parsing file.\n");
  }
  
  return 0;
}

Open in new window

0
LearningCppAuthor Commented:
Thanks the given function worked, :)
But i need to find out the size of GIF image also.

What is the solution for it?
0
AndyAinscowFreelance programmer / ConsultantCommented:
Something like this should be fine.

CImage img;
img.Load("file path goes here ");
int w = img.GetWidth();
int h = img.GetHeight();



erm - didn't I say that already?
0
LearningCppAuthor Commented:
it give compilation error as "'CImage' : undeclared identifier".
Any header file is required for it?
0
AndyAinscowFreelance programmer / ConsultantCommented:
Try
#include "atlimage.h"



Note - you might be using an older version of MFC that doesn't support the CImage class
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
LearningCppAuthor Commented:
Hey great, it worked well.
Thanks for instant reply.
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
System Programming

From novice to tech pro — start learning today.