Skip to content

Imaging.CreateBitmapSourceFromHIcon crash: Value does not fall within the expected range. #10397

@n9

Description

@n9

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

miloush commented on Feb 4, 2025

@miloush
Contributor

Well spotted!

miloush

miloush commented on Feb 4, 2025

@miloush
Contributor

Are you interested in creating a PR with a fix?

n9

n9 commented on Feb 4, 2025

@n9
Author

I am unable to find InteropBitmap.cs in this repo.

miloush

miloush commented on Feb 4, 2025

@miloush
Contributor
miloush

miloush commented on Feb 4, 2025

@miloush
Contributor

(you can search for InteropBitmap here on GitHub)

added a commit that references this issue on Feb 4, 2025
n9

n9 commented on Feb 4, 2025

@n9
Author

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

miloush commented on Feb 4, 2025

@miloush
Contributor

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).

added a commit that references this issue on Feb 4, 2025
n9

n9 commented on Feb 4, 2025

@n9
Author

I don't think you need the empty rectangle check, because the GetScaledWidthAndHeight method takes care of the case.

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.

We might need to tweak things a bit or start with a known good branch easy for testing (such as 9.0 release).

I rebased the branch (commit) on the release/9.0 branch.

You can try to run build from the command line of the cloned repository. Does it build?

It failed:

  main.cpp
  UIAutomationClientSideProviders-ref -> L:\Foreign\.net\wpf\artifacts\bin\UIAutomationClientSideProviders-ref\Debug\ne
  t9.0\UIAutomationClientSideProviders.dll
  PresentationCore-ref -> L:\Foreign\.net\wpf\artifacts\bin\PresentationCore-ref\Debug\net9.0\PresentationCore.dll
  Generating Code...
  .NETCoreApp,Version=9.0.AssemblyAttributes.cpp
LINK : fatal error LNK1104: cannot open file 'MSVCURTD_netcore.LIB' [L:\Foreign\.net\wpf\src\Microsoft.DotNet.Wpf\src\D
irectWriteForwarder\DirectWriteForwarder.vcxproj]
h3xds1nz

h3xds1nz commented on Feb 4, 2025

@h3xds1nz
Member

It failed:

  main.cpp
  UIAutomationClientSideProviders-ref -> L:\Foreign\.net\wpf\artifacts\bin\UIAutomationClientSideProviders-ref\Debug\ne
  t9.0\UIAutomationClientSideProviders.dll
  PresentationCore-ref -> L:\Foreign\.net\wpf\artifacts\bin\PresentationCore-ref\Debug\net9.0\PresentationCore.dll
  Generating Code...
  .NETCoreApp,Version=9.0.AssemblyAttributes.cpp
LINK : fatal error LNK1104: cannot open file 'MSVCURTD_netcore.LIB' [L:\Foreign\.net\wpf\src\Microsoft.DotNet.Wpf\src\D
irectWriteForwarder\DirectWriteForwarder.vcxproj]

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

n9 commented on Feb 4, 2025

@n9
Author

VS Developer prompt does not help:

Image

miloush

miloush commented on Feb 4, 2025

@miloush
Contributor

the msvcurtd_netcore.lib is part of the MSVC, perhaps you are missing some of the components of VS from the developer guide?

n9

n9 commented on Feb 4, 2025

@n9
Author

Maybe. I just cloned the repo, opened in VS (it installed MSVC). And then after suggestion run build.cmd in the VS Developer prompt.

Image

If I create a draft of PR, will Github build and test it?

12 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugProduct bug (most likely)

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @n9@miloush@ThomasGoulet73@h3xds1nz@himgoyalmicro

    Issue actions

      Imaging.CreateBitmapSourceFromHIcon crash: Value does not fall within the expected range. · Issue #10397 · dotnet/wpf