dotnet/wpf · error · ArgumentException

Image_NoDecodeFrames (stream)

Error message

Image_NoDecodeFrames (stream)

What it means

BitmapFrame.Create(Stream, ...) throws ArgumentException with SR.Image_NoDecodeFrames when decoding the supplied stream produces a decoder with zero frames. The stream content either is not decodable image data or ends before any frame is present.

Solutions

  1. Ensure the stream is positioned at 0 (stream.Position = 0) and is non-empty before calling Create.
  2. Validate the stream's magic bytes (PNG \x89PNG, JPEG FFD8, etc.) before decoding.
  3. Wrap the stream in a BitmapDecoder and check Frames.Count before extracting a frame.
  4. Use BitmapCacheOption.OnLoad so the entire stream is read and buffered at decode time.

Example fix

// before
var frame = BitmapFrame.Create(responseStream);

// after
responseStream.Position = 0;
if (responseStream.Length == 0) throw new InvalidDataException("Empty image stream");
var dec = BitmapDecoder.Create(responseStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
var frame = dec.Frames.Count > 0 ? dec.Frames[0] : null;
Defensive patterns

Strategy: validation

Validate before calling

if (stream == null || !stream.CanRead) throw new ArgumentException("stream");
stream.Position = 0;
if (stream.Length == 0) throw new InvalidDataException("Empty image stream");

Type guard

bool isDecodableStream(Stream s) { try { s.Position = 0; return BitmapDecoder.Create(s, BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.Count > 0; } catch { return false; } }

Try / catch

try { frame = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); }
catch (ArgumentException ex) when (ex.ParamName == "stream") { /* handle undecodable stream */ }

Prevention

When it happens

Trigger: BitmapFrame.Create(stream) or Create(stream, createOptions, cacheOption) with a stream whose decoded Frames.Count == 0 (empty stream, wrong format, truncated data).

Common situations: Passing an HTTP response stream that contains an error body instead of image bytes; MemoryStream positioned at End or zero-length; uploading a corrupted file.

Understand the failure class

Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f147987c5c2bf543. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapFrame.cs:80

                {
                    throw new System.ArgumentException(SR.Image_NoDecodeFrames, nameof(uri));
                }

                return decoder.Frames[0];
            }
            else
            {
                Debug.Assert((stream != null), "Both stream and uri are null");

                BitmapDecoder decoder = BitmapDecoder.Create(
                    stream,
                    createOptions,
                    cacheOption
                    );

                if (decoder.Frames.Count == 0)
                {
                    throw new System.ArgumentException(SR.Image_NoDecodeFrames, nameof(stream));
                }

                return decoder.Frames[0];
            }
        }

        /// <summary>
        /// Create a BitmapFrame from a Uri using BitmapCreateOptions.None and
        /// BitmapCacheOption.Default
        /// </summary>
        /// <param name="bitmapUri">Uri of the Bitmap</param>
        public static BitmapFrame Create(
            Uri bitmapUri
            )
        {
            return Create(bitmapUri, null);
        }
        

View on GitHub (pinned to 81131a70a4)