When displaying bitmap images in WPF, the system wants to display all images in 96 dpi. WPF is one of the few environments where the file’s DPI is used in calculating the display DPI. The images that I was working with had 500 dpi and the images were displaying blurry since they were being downsized to 72.
I haven’t found a way to override the output resolution of an image, so instead, I hack the resolution of the image when it is being read. Bytes 38/39 is the Horizontal DPI and 42/43 is the Vertical DPI.
To set it to 72 DPI, we must first realize that the resolution is in meters. Google’s online conversion app says that 3780 inches = 96 meters. So we set our resolution to 0x0EC4!
var bytes = File.ReadAllBytes(fileName); bytes[38] = 0xc4; // HLL to 3780 (96 dpi) bytes[39] = 0x0e; bytes[42] = 0xc4; // VLL to 3780 (96 dpi) bytes[43] = 0x0e; var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.Default; bitmapImage.StreamSource = new MemoryStream(bytes); bitmapImage.EndInit();