dotnet/wpf · error · NotSupportedException

Image_NoFrames

Error message

Image_NoFrames

What it means

BitmapEncoder.Save requires at least one frame before encoding; if _frames is null or its count is <= 0, NotSupportedException(Image_NoFrames) is thrown, since a bitmap container must contain at least one frame to write.

Solutions

  1. Always add at least one BitmapFrame before Save: encoder.Frames.Add(BitmapFrame.Create(bitmapSource))
  2. Verify the source BitmapSource decoded successfully before frame creation
  3. Check Frames.Count > 0 before calling Save
  4. Handle decode failures upstream so frame addition is never skipped silently

Example fix

// before
var encoder = new PngBitmapEncoder();
encoder.Save(ms); // Image_NoFrames
// after
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
encoder.Save(ms);
Defensive patterns

Strategy: validation

Validate before calling

if (encoder.Frames == null || encoder.Frames.Count == 0)
    encoder.Frames.Add(BitmapFrame.Create(sourceBitmap));
encoder.Save(stream);

Type guard

static bool HasFrames(BitmapEncoder e) => e.Frames != null && e.Frames.Count > 0;

Try / catch

try { encoder.Save(stream); }
catch (NotSupportedException ex) when (IsNoFrames(ex)) {
    log.Error("No frames added before Save", ex);
    encoder.Frames.Add(BitmapFrame.Create(fallbackFrame));
    encoder.Save(stream); }

Prevention

When it happens

Trigger: Calling encoder.Save() without ever adding a frame via encoder.Frames.Add(BitmapFrame.Create(...)) — or after Add calls silently failed — from Save itself or its internal callers (GetBitmapImpl, CreateImagePart, CreateBrushImage, ReEncodeImage, ConvertTo).

Common situations: Constructing an encoder but forgetting to add frames; conditionally adding a frame whose source was null/failed to decode; generic helper that creates the encoder but the frame-adding branch was skipped.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapEncoder.cs:332

        public virtual void Save(System.IO.Stream stream)
        {
            VerifyAccess();
            EnsureBuiltIn();
            EnsureUnmanagedEncoder();

            // No-op to get rid of build error
            if (_encodeState == EncodeState.None)
            {
            }

            if (_hasSaved)
            {
                throw new InvalidOperationException(SR.Image_OnlyOneSave);
            }

            if (_frames == null)
            {
                throw new System.NotSupportedException(SR.Format(SR.Image_NoFrames, null));
            }

            int count = _frames.Count;
            if (count <= 0)
            {
                throw new System.NotSupportedException(SR.Format(SR.Image_NoFrames, null));
            }

            IntPtr comStream = IntPtr.Zero;
            SafeMILHandle encoderHandle = _encoderHandle;

            try
            {
                comStream = StreamAsIStream.IStreamFrom(stream);

                // does this addref the stream?
                HRESULT.Check(UnsafeNativeMethods.WICBitmapEncoder.Initialize(
                    encoderHandle,

View on GitHub (pinned to 81131a70a4)