From 94ba24a03ef781af5c86ee51df11ef3d677c5772 Mon Sep 17 00:00:00 2001 From: lindexi Date: Sun, 1 Aug 2021 18:46:59 +0800 Subject: [PATCH] Fix create BitmapDecoder with async file stream. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [BitmapDecoder.Create does not handle FileStream with FileOptions.Asynchronous · Issue #4355 · dotnet/wpf](https://github.com/dotnet/wpf/issues/4355 ) --- .../System/Windows/Media/Imaging/BitmapDecoder.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDecoder.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDecoder.cs index 165353e52..ba6e0c2cb 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDecoder.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDecoder.cs @@ -1138,14 +1138,22 @@ out SafeFileHandle safeFilehandle if (stream is System.IO.FileStream) { System.IO.FileStream filestream = stream as System.IO.FileStream; - try { - safeFilehandle = filestream.SafeFileHandle; + if (filestream.IsAsync is false) + { + safeFilehandle = filestream.SafeFileHandle; + } + else + { + // If Filestream is async that doesn't support IWICImagingFactory_CreateDecoderFromFileHandle_Proxy, then revert to old code path. + safeFilehandle = null; + } } catch { // If Filestream doesn't support SafeHandle then revert to old code path. + // See https://github.com/dotnet/wpf/issues/4355 safeFilehandle = null; } }