1

I have this function to check PNG color depth:

MemoryStream stream = new MemoryStream(File.ReadAllBytes(path));
var source = BitmapFrame.Create(stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnDemand);
return source.Format.BitsPerPixel;

Whenever this code runs, the DPI scaling of the application will break and resets to 100%, and the window is not maximized anymore.

I've managed to isolate the line of code that causes this, and it's BitmapFrame.Create().

This even happens if I skip the code entirely, like this:

return 32;
MemoryStream stream = new MemoryStream(File.ReadAllBytes(path));
var source = BitmapFrame.Create(stream, BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnDemand);
return source.Format.BitsPerPixel;

None of the other lines after "return 32" will run, yet the bug still gets triggered if I run the function. It only works if I actually comment the lines out.

Any ideas? I simply don't understand how this is even possible, which makes it hard to debug.

nmkd
  • 306
  • 3
  • 9
  • [DPI Awareness - Unaware in one Release, System Aware in the Other](https://stackoverflow.com/a/50276714/7444103). The sum of it is that when you reference `PresentationFramework` assemblies in a non-DpiAware WinForms app, DpiAwaress can be automatically enabled (System-Aware) for some or all Forms. You can make your app DpiAware (preferable), or use the `System.Windows.Media.DisableDpiAwareness` override to disable the automatic feature. – Jimi Oct 15 '20 at 09:10

0 Answers0