问题
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