Question

For example, C # says that the selected image contains 96 ppi, while that same image in Photoshop contains 72 ppi.

Why is there a difference?

I’m inclined to trust Photoshop in this case, and how to test image resolution if C# returns false results?

We need to build some sort of validator control that rejects all images with ppi != 300.

Control should support the following formats: jpg, jpeg, gif, png, bmp.

Code is listed below:

Image i = Image.FromFile(FileName);

Console.Write(i.VerticalResolution);
Console.Write(i.HorizontalResolution);
Was it helpful?

Solution

DPI means dots (pixels) per inch. The physical size in inches is subjective, based on the current monitor's size and resolution. Unless you're relying on metadata (which gif and bmp don't contain) you cannot reliably calculate this.

Photoshop simply has a prescribed value for DPI, which it uses when translating images for print. This value is stored in the PSD file and may be copied to JPEG metadata, but if you save the image in a format without DPI metadata, the information is not stored.

Update:

The reason your code gets a different value is that C# fetches its VerticalResolution and HorizontalResolution values from the current DPI setting on the computer. Photoshop's DPI is for use with print, so it knows the physical dimensions if you want to send your image to a printer. It has a default value of 72dpi, but you can change this. The value has no meaning on a screen, though, since screens deal in pixels only.

OTHER TIPS

DPI means dots per inch. A bitmap image does not have an inherent DPI, it merely has a size which is the number of pixels in the horizontal and the number of pixels in the vertical (width and height). An image only gains a resolution (in DPI) when you say how many pixels you want to squeeze into each inch.

So if I have an image that is 100 pixels wide and 100 pixels high (100px × 100px), it will be 100 DPI if I print it (or convert it into a format that dictates the print size) such that it fits exactly in a one square inch (1" × 1"). It will be 50 DPI if I print it to fit in a square that is two inches by two inches, &c.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top