Skip to content

BitmapDecoder.Create does not handle FileStream with FileOptions.Asynchronous #4355

@vonzshik

Description

@vonzshik
  • .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);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions