Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the resolution of a jpeg image using C# and the .NET Environment?

Our clients will be uploading images to be printed on their documents and we have been asked to come up with a way to get the resolution of the image in order to warn them if the image has too low of a resolution and will look pixalated in the end-product

If it comes to it we could also go with the dimensions if anyone knows how to get those but the resolution would be preferred

Thank you

like image 209
Josh Mein Avatar asked Sep 23 '08 20:09

Josh Mein


3 Answers

System.Drawing.Image

Image newImage = Image.FromFile("SampImag.jpg");
newImage.HorizontalResolution
like image 83
Xian Avatar answered Oct 03 '22 02:10

Xian


It depends what you are looking for... if you want the DPI of the image then you are looking for the HorizontalResolution which is the DPI of the image.

Image i = Image.FromFile(@"fileName.jpg");
i.HorizontalResolution;

If you want to figure out how large the image is then you need to calculate the measurements of the image which is:

int docHeight = (i.Height / i.VerticalResolution);
int docWidth = (i.Width / i.HorizontalResolution);

This will give you the document height and width in inches which you could then compare to the min size needed.

like image 36
Brian ONeil Avatar answered Oct 03 '22 00:10

Brian ONeil


DPI take sense when printing only. 72dpi is the Mac standard and 96dpi is the Windows standard. Screen resolution only takes pixels into account, so a 72dpi 800x600 jpeg is the same screen resolution than a 96dpi 800x600 pixels.

Back to the '80s, Mac used 72dpi screen/print resolution to fit the screen/print size, so when you had an image on screen at 1:1, it correspond to the same size on the printer. Windows increased the screen resolution to 96dpi to have better font display.. but as a consequence, the screen image doesn't fit the printed size anymore.

So, for web project, don't bother with DPI if the image isn't for print; 72dpi, 96dpi, even 1200dpi should display the same.

like image 40
Gabriel Mailhot Avatar answered Oct 03 '22 02:10

Gabriel Mailhot