Having BitmapFrame.Create() in my code breaks Winforms DPI scaling - Even if the code doesn't run

44 views Asked by At

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.

0

There are 0 answers