The Cloud One Blog

Posted by admin on Wednesday, June 27, 2012

C# get resolution from image file

We had a customer for printing canvas business, and we had to find the way to get the DPI (resolution) of the file in order to suggest if the picture file was suitable for printing at a chosen canvas size. Here is the code we used in case is useful for someone else.

You can get both, resolution (DPI - Dot Per Inch) and width and height, using the Image class.

1. Include the drawing library

using System.Drawing;

2. To get the resolution (in dot per inch):

Image i = Image.FromFile("file.jpg");
int imgResolution = (int)i.HorizontalResolution;

3. To get the width and height (in pixels):

int imgHeight = i.Height;
int imgWidth = i.Width;

4. To get the width and height (in inches):

float docHeight = i.Height / i.VerticalResolution;
float docWidth = i.Width / i.HorizontalResolution;