Skip to content

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

Open
@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

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

No branches or pull requests

Issue actions