Description
Description
Imaging.CreateBitmapSourceFromHIcon
, when called with BitmapSizeOptions.FromHeight(16)
, throws ArgumentException: Value does not fall within the expected range.
Reproduction Steps
// just create a hIcon
var hIcon = new System.Drawing.Bitmap(64, 64).GetHicon();
// this will crash
var bitmapSource = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromHeight(16));
Expected behavior
The above code will not throw an exception.
Actual behavior
The code above throws ArgumentException: Value does not fall within the expected range.
Regression?
It does not seem to be.
Known Workarounds
Do not use Imaging.CreateBitmapSourceFromHIcon
with explicit BitmapSizeOptions
.
Impact
Any API that uses InteropBitmap
with explicit BitmapSizeOptions
may be affected.
Configuration
.NET 9 / Win10 / x64
It may not, as the problem is in the managed code.
Other information
The problem seems to be caused by the InteropBitmap.FinalizeCreation
method that calls:
_sizeOptions.GetScaledWidthAndHeight((uint)_sizeOptions.PixelWidth, (uint)_sizeOptions.PixelHeight, out var newWidth, out var newHeight);
I assume that the first two arguments of BitmapSizeOptions.GetScaledWidthAndHeight
should contain the original image size instead of _sizeOptions.PixelWidth
and _sizeOptions.PixelHeight
.
Activity
miloush commentedon Feb 4, 2025
Well spotted!
miloush commentedon Feb 4, 2025
Are you interested in creating a PR with a fix?
n9 commentedon Feb 4, 2025
I am unable to find
InteropBitmap.cs
in this repo.miloush commentedon Feb 4, 2025
wpf/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/InteropBitmapSource.cs
Line 22 in 43f4bc5
miloush commentedon Feb 4, 2025
(you can search for InteropBitmap here on GitHub)
Draft for dotnet#10397
n9 commentedon Feb 4, 2025
I cloned the repo and opened it in VS. Now it is much easier.
I have mocked up a solution: n9@623fc97, but I do not know, how to test it.
(I don't have much experience with WPF or its internals.)
miloush commentedon Feb 4, 2025
That's a good start. I don't think you need the empty rectangle check, because the GetScaledWidthAndHeight method takes care of the case.
There is a guide at https://github.com/dotnet/wpf/blob/main/Documentation/developer-guide.md though it doesn't look very friendly to me.
You can try to run
build
from the command line of the cloned repository. Does it build? We might need to tweak things a bit or start with a known good branch easy for testing (such as 9.0 release).Draft for dotnet#10397
n9 commentedon Feb 4, 2025
If you mean this condition:
https://github.com/n9/wpf/blob/1467b5df5bee57c5cd0f179ec03d271bdb6201d1/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/InteropBitmapSource.cs#L391-L400
I do not understand, how
GetScaledWidthAndHeight
can handle it.I rebased the branch (commit) on the release/9.0 branch.
It failed:
h3xds1nz commentedon Feb 4, 2025
That started like two weeks ago; using VS Developer prompt and running the build from there should work just fine though.
Or you can just build it directly if you use the
start-vs.cmd
in preview, that way you can also run tests via Test Explorer.n9 commentedon Feb 4, 2025
VS Developer prompt does not help:
miloush commentedon Feb 4, 2025
the msvcurtd_netcore.lib is part of the MSVC, perhaps you are missing some of the components of VS from the developer guide?
n9 commentedon Feb 4, 2025
Maybe. I just cloned the repo, opened in VS (it installed MSVC). And then after suggestion run
build.cmd
in the VS Developer prompt.If I create a draft of PR, will Github build and test it?
12 remaining items