BitmapDecoder.Create does not handle FileStream with FileOptions.Asynchronous
See original GitHub issue- .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.
Minimal repro:
using var fs = new FileStream("image.jpg",
FileMode.Open,
FileAccess.Read,
FileShare.Read,
4096,
FileOptions.Asynchronous);
var decoder = BitmapDecoder.Create(fs, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Not sure is the correct way in handling file asynchronously
There is another issue. A FileStream must be opened in async mode by using a constructor overload that takes FileOptions.Asynchronous in order ...
Read more >Asynchronous file access (C#)
Learn how to use the async feature to access files in C#. You can call into asynchronous methods without using callbacks or splitting...
Read more >WPF 已知问题BitmapDecoder.Create 不支持传入 ... - 林德熙
GitHub 链接: BitmapDecoder.Create does not handle FileStream with FileOptions.Asynchronous · Issue #4355 · dotnet/wpf. 现象是传入BitmapDecoder.
Read more >WPF 已知问题BitmapDecoder.Create 不支持传入 ... - 腾讯云
GitHub 链接: BitmapDecoder.Create does not handle FileStream with FileOptions.Asynchronous · Issue #4355 · dotnet/wpf.
Read more >Open FileStream properly for asynchronous reading/writing
On Windows, when you want to open/create a HANDLE (which the file ultimately is) and use overlapped operations (which is just a different...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@vonzshik I fixed it, see https://github.com/dotnet/wpf/pull/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.
We have taken this PR for the current Community Test Pass. Thanks for the contribution.