You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BitmapDecoder.Create does not handle FileStream with FileOptions.Asynchronous #4355
.NET Core Version: (e.g. 3.0 Preview1, or daily build number, use dotnet --info)
5.0.201
Windows version: (winver)
19042.867
Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes/No
Yes.
Problem description:
BitmapDecoder.Create throws ArgumentException ("Value does not fall within the expected range.") whenever the stream argument is FileStream with FileOptions.Asynchronous.
Actual behavior:
at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
at System.Windows.Media.Imaging.BitmapDecoder.Create(Stream bitmapStream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption)
Expected behavior:
BitmapDecoder.Create shouldn't throw an exception.
The BitmapDecoder.Create will call the IWICImagingFactory_CreateDecoderFromFileHandle_Proxy which in WIC to create the decoder. But the IWICImagingFactory_CreateDecoderFromFileHandle_Proxy can not receive the Asynchronous SafeFilehandle.
@lindexi thank you for taking a look. I assume there is no API for an async SafeFileHandle, yes?
The fact that I was able to get around this issue by making a proxy stream, which wraps the original FileStream (and the API you've mentioned does confirm my suspicions), makes me think that BitmapDecoder.Create does check whether a provided stream is indeed a FileStream, in which case it uses a different set of API (I guess as a sort of optimization?), unlike for other cases.
If so, maybe it's a good idea to disable this behavior whenever FileStream.IsAsync is true? Otherwise, I think making the exception more verbose will surely help solving this problem for other users.
Activity
lindexi commentedon May 14, 2021
The
BitmapDecoder.Createwill call theIWICImagingFactory_CreateDecoderFromFileHandle_Proxywhich in WIC to create the decoder. But the IWICImagingFactory_CreateDecoderFromFileHandle_Proxy can not receive the Asynchronous SafeFilehandle.IWICImagingFactory_CreateDecoderFromFileHandle_Proxy function - Win32 apps | Microsoft Docs
vonzshik commentedon May 14, 2021
@lindexi thank you for taking a look. I assume there is no API for an async
SafeFileHandle, yes?The fact that I was able to get around this issue by making a proxy stream, which wraps the original
FileStream(and the API you've mentioned does confirm my suspicions), makes me think thatBitmapDecoder.Createdoes check whether a provided stream is indeed aFileStream, in which case it uses a different set of API (I guess as a sort of optimization?), unlike for other cases.If so, maybe it's a good idea to disable this behavior whenever
FileStream.IsAsyncistrue? Otherwise, I think making the exception more verbose will surely help solving this problem for other users.lindexi commentedon May 14, 2021
@vonzshik I agree with you.
The IWICImagingFactory_CreateDecoderFromFileHandle_Proxy do not suppo…
lindexi commentedon May 15, 2021
I write a demo to show the
IWICImagingFactory_CreateDecoderFromFileHandle_Proxydo not support the Asynchronous file stream.See https://github.com/lindexi/lindexi_gd/tree/78c73fe25229f0b50992102e59c01cd535e60c31/JemlemlacuLemjakarbabo
lindexi commentedon May 15, 2021
I write a Chinese blog about this, see https://translate.google.com/translate?sl=zh-CN&tl=en&u=https://blog.lindexi.com/post/WPF-%25E5%25B7%25B2%25E7%259F%25A5%25E9%2597%25AE%25E9%25A2%2598-BitmapDecoder.Create-%25E4%25B8%258D%25E6%2594%25AF%25E6%258C%2581%25E4%25BC%25A0%25E5%2585%25A5-Asynchronous-%25E7%259A%2584%25E6%2596%2587%25E4%25BB%25B6%25E6%25B5%2581.html
Fix create BitmapDecoder with async file stream.
Fix create BitmapDecoder with async file stream.
[BitmapDecoder.Create does not handle FileStream with FileOptions.Asy…
lindexi commentedon Aug 2, 2021
@vonzshik I fixed it, see #4966
I follow your opinion, when I judge this file is asynchronous, I read the file into memory.
I wrote a demo code, and it work well. See https://github.com/lindexi/lindexi_gd/tree/20b78c5547b2ecacdd244a438ef58518469a91d7/GeafakaijawkeheahemLejeehocear
You can test the demo by publish the code with self contained.
Fix create BitmapDecoder with async file stream.
dipeshmsft commentedon Aug 10, 2022
We have taken this PR for the current Community Test Pass. Thanks for the contribution.
Fix create BitmapDecoder with async file stream. (#4966)