# How to find the resolution of an image in C++

We have a collection of images of different resolutions. So today we are going to find the resolution of a particular image using C++.

## Resolution of an image in C++

Resolution is image quality produced by display or printer. It is also measured by pixels. Example is 1680X1050 resolution etc. Resolution is a deciding factor for image size. We can improve the image quality of an image by increasing it resolution. There are various processing of checking the resolution of an image. And by one of the methods using C++, we can find the resolution of an image by using our code.

### Code:

```#include <iostream>
#include <fstream>

using namespace std;

bool GetImageSize(const char *fn, int *x,int *y)
{
FILE *f=fopen(fn,"rb");
if (f==0) return false;
fseek(f,0,SEEK_END);
long len=ftell(f);
fseek(f,0,SEEK_SET);
if (len<24) {
fclose(f);
return false;
}
cout << fn << endl;

if (buf==0xFF && buf==0xD8 && buf==0xFF && buf==0xE0 && buf=='J' && buf=='F' && buf=='I' && buf=='F')
{ long pos=2;
while (buf==0xFF)
{ if (buf==0xC0 || buf==0xC1 || buf==0xC2 || buf==0xC3 || buf==0xC9 || buf==0xCA || buf==0xCB) break;
pos += 2+(buf<<8)+buf;
if (pos+12>len) break;
}
}

fclose(f);

if (buf==0xFF && buf==0xD8 && buf==0xFF)
{ *y = (buf<<8) + buf;
*x = (buf<<8) + buf;
return true;
}

if (buf=='G' && buf=='I' && buf=='F')
{ *x = buf + (buf<<8);
*y = buf + (buf<<8);
return true;
}

if ( buf==0x89 && buf=='P'&&buf=='N'&& buf=='G'&&buf==0x0D&&buf==0x0A&&buf==0x1A&&buf==0x0A&&buf=='I'&&buf=='H'&&buf=='D'&& buf=='R')
{ *x = (buf<<24) + (buf<<16) + (buf<<8) + (buf<<0);
*y = (buf<<24) + (buf<<16) + (buf<<8) + (buf<<0);
return true;
}

return false;
}

int main()
{

const char *theFile = "11.png";
int the_x =0;
int the_y =0;
bool didRun = false;

didRun = GetImageSize(theFile, &the_x, &the_y);

cout << "resolution: " << the_x << " x " << the_y << endl;
return 0;
}
```

#### Output:

```11.png
resolution: 143 x 120```

In this output first, we get the image i.e. 11.png. And then we are going to get the resolution of an image.