Search code examples
c#imagephotoshopppi

The difference in image resolution (ppi) between C# and Photoshop


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);

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.