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

耗尽温柔 提交于 2019-12-13 07:37:27

问题


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#?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/12485764/how-can-i-find-the-pixel-per-inch-value-in-a-jpg-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!