简体   繁体   中英

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

我试图验证某些图像不允许每英寸低于300像素的图像,有没有办法在使用C#的ASP.NET上找到它?

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. Ie 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM