question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
lindexicommented, Aug 2, 2021

@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.

dotnet publish --self-contained -r win-x86
1reaction
dipeshmsftcommented, Aug 10, 2022

We have taken this PR for the current Community Test Pass. Thanks for the contribution.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found