Cannot freeze color palette based BitmapSource image
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
* .NET Core Version: 5.0.201
* Windows version: Windows 10x64 20H2 19042.906
* Does the bug reproduce also in WPF for .NET Framework 4.8?
I'm not able to test: [.NET Framework 4.8 not available in VS 2019 New Project wizard](https://developercommunity.visualstudio.com/t/NET-Framework-48-not-listed-in-New-Pro/1395034?entry=myfeedback&space=8&scope=follow&sort=newest)
However, I can reproduce in .NET Framework 4.72.
* Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? No.
### Problem description:
I'm not able to freeze a color palette based [`BitmapSource`](https://docs.microsoft.com/dotnet/api/system.windows.media.imaging.bitmapsource) image.
So, it's impossible to create a color palette based `BitmapSource` image asynchronously and yield to WPF UI thread. (See [this issue on StackOverflow](https://stackoverflow.com/questions/67020257/how-can-i-yield-a-palette-image-from-another-thread-to-wpf-ui-thread).)
### Actual behavior:
```BitmapSource.CanFreeze``` always yields `false` when the image assigned is using a color palette.
### Expected behavior:
```BitmapSource.CanFreeze``` should return `true`, even with bitmaps using a color palette.
A color palette is a `DispatcherObject` that cannot be transferred between threads, cloned or anything else. So, there is no way to workaround this situation.
### Minimal repro:
```c#
internal static Task GetImageAsync()
{
return Task.Run(() =>
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"test.jpg");
bi.DecodePixelWidth = 16;
bi.EndInit();
FormatConvertedBitmap fcb = new FormatConvertedBitmap(bi, PixelFormats.Indexed2, new BitmapPalette(bi, 4), 1);
// Required for the UI thread to be able to use the bitmap.
// However, fcb.CanFreeze is false, though.
fcb.Freeze();
return fcb;
});
}
```
WPF tracing returns the following warning message:
```html
System.Windows.Freezable Warning:
2 : CanFreeze is returning false because a DependencyProperty
on the Freezable has a value that is a DispatcherObject
with thread affinity
```
Contributor guide
Assessment
This issue has not been assessed yet.