#277 – Images May Have Embedded DpiX and DpiY Information

Even when you specify Stretch=None for an Image control, you may notice that the image does not appear at the expected number of pixels wide/high.  This can happen if the image’s resolution (DPI) doesn’t match your current system DPI.

An image file (e.g. .png or .jpg) contains an image that is a certain number of pixels wide/high.  An image file may also contain embedded DpiX and DpiY (resolution) information.  This resolution information is not present in all image files.

WPF uses this resolution information to calculate the image size in WPF Units as:

  • Width (in WPF Units) = (96 * ImageWidth) / ImageHorizontalDPI
  • Height (in WPF Units) = (96 * ImageHeight) / ImageVerticalDPI

If there is no resolution information in the file, WPF defaults to 96 dpi.

WPF then calculates the image size in pixels as:

  • Width (pixels) = Width (WPF Units) * (SystemDPI / 96)
  • Height (pixels) = Height (WPF Units) * (SystemDPI / 96)