How can I find the pixel per inch value in a JPG i

2019-08-17 03:59发布

I am trying to validate certain images to not allow images lower than 300 pixels per inch, is there a way to find it on ASP.NET using C#?

2条回答
SAY GOODBYE
2楼-- · 2019-08-17 04:23

You've got to read EXIF data from the image.

Here you have an example of how you can do it, using ExifLib

ExifLib - A Fast Exif Data Extractor for .NET 2.0+

Be warned that not all jpeg images have the resolution information. And, that even if they have it, you can print them using a completely different resolution. I.e. a pic 200px wide can be printed using 1 inch width is 200dpi. This same image printed using 2 inches is 100dpi, and using 1/2 inch is 400dpi.

EDIT: It's even possible to get this info with native .NET framework Image.PropertyItems Property

查看更多
贪生不怕死
3楼-- · 2019-08-17 04:28

The Image object of the .NET Framework will give you the PPI of a Bitmap (including a JPG).

Image image = new Bitmap(@"C:\myimage.jgp");
float ppi = image.HorizontalResolution; // the image's pixels per inch
float widthInInches = image.PhysicalDimension.Width / ppi;

Seems to work for me. I was able to discern that a specific image I am using in a PDF is 90 ppi.

查看更多
登录 后发表回答