[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 692
  • Last Modified:

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?
0
LearningCpp
Asked:
LearningCpp
  • 3
  • 3
  • 2
  • +1
1 Solution
 
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
 
AndyAinscowCommented:
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
Try
#include "atlimage.h"



Note - you might be using an older version of MFC that doesn't support the CImage class
0
 
LearningCppAuthor Commented:
Hey great, it worked well.
Thanks for instant reply.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now