BitmapImage: app crash with FileFormatException when loading specific image and DecodePixelWidth
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
* .NET Core Version: `5.0.100-preview.8.20417.9`
* Windows version: `Windows 10 2004 Build 19041.508`
* Does the bug reproduce also in WPF for .NET Framework 4.8?: `Yes`
**Problem description:**
App crashes with `FileFormatException` when loading specific image using `BitmapImage` with `DecodePixelWidth = 32`.
This affects .NETFramework as well as .NET Core.
Known affected products:
- NuGetPackageExplorer
- NuGet Package Management in Visual Studio
See also https://github.com/NuGetPackageExplorer/NuGetPackageExplorer/issues/1105
**Actual behavior:**
App crashes:
```
System.IO.FileFormatException
HResult=0x80131537
Message=Der Bitmap-Farbkontext ist ungültig.
Source=PresentationCore
StackTrace:
at System.Windows.Media.Imaging.ColorConvertedBitmap.FinalizeCreation()
at System.Windows.Media.Imaging.ColorConvertedBitmap..ctor(BitmapSource source, ColorContext sourceColorContext, ColorContext destinationColorContext, PixelFormat format)
at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
at System.Windows.Media.Imaging.BitmapImage.OnDownloadCompleted(Object sender, EventArgs e)
at System.Windows.Media.UniqueEventHelper.InvokeEvents(Object sender, EventArgs args)
at System.Windows.Media.Imaging.LateBoundBitmapDecoder.DownloadCallback(Object arg)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at System.Windows.Window.ShowDialog()
at PackageExplorer.PackageChooserDialog.ShowDialog(String searchTerm) in C:\dev_projects\NuGetPackageExplorer\PackageExplorer\PackageChooser\PackageChooserDialog.xaml.cs:line 152
Inner Exception 1:
COMException: Das angegebene Farbprofil ist ungültig. (0x800707DB)
```
**Expected behavior:**
No crash and invoke `DecodeFailed` event with this exception instead.
**Minimal repro:**
```c#
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri("https://cdn.statically.io/gh/Starz0r/ChocolateyPackagingScripts/2055976c/assets/legendary.png");
bitmap.DecodePixelWidth = 32;
bitmap.EndInit();
// set source of an
image.Source = bitmap;
```
Contributor guide
Assessment
This issue has not been assessed yet.