dotnet / dotnet/wpf

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

Open
#10,397 18 comments 0 reactions 2 assignees Claimed by @h3xds1nz View on GitHub
Bug
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

`Imaging.CreateBitmapSourceFromHIcon`, when called with `BitmapSizeOptions.FromHeight(16)`, throws ArgumentException: Value does not fall within the expected range.

### Reproduction Steps

```C#
// 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`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.