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?
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
Try
#include "atlimage.h"



Note - you might be using an older version of MFC that doesn't support the CImage class
0
 
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
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
 
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
 
LearningCppAuthor Commented:
Hey great, it worked well.
Thanks for instant reply.
0
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.

All Courses

From novice to tech pro — start learning today.