{"record":{"id":"5ef359bbadc63b29","repo":"dotnet/wpf","slug":"image-noframes","errorCode":null,"errorMessage":"Image_NoFrames","messagePattern":"Image_NoFrames","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapEncoder.cs","lineNumber":332,"sourceCode":"        public virtual void Save(System.IO.Stream stream)\n        {\n            VerifyAccess();\n            EnsureBuiltIn();\n            EnsureUnmanagedEncoder();\n\n            // No-op to get rid of build error\n            if (_encodeState == EncodeState.None)\n            {\n            }\n\n            if (_hasSaved)\n            {\n                throw new InvalidOperationException(SR.Image_OnlyOneSave);\n            }\n\n            if (_frames == null)\n            {\n                throw new System.NotSupportedException(SR.Format(SR.Image_NoFrames, null));\n            }\n\n            int count = _frames.Count;\n            if (count <= 0)\n            {\n                throw new System.NotSupportedException(SR.Format(SR.Image_NoFrames, null));\n            }\n\n            IntPtr comStream = IntPtr.Zero;\n            SafeMILHandle encoderHandle = _encoderHandle;\n\n            try\n            {\n                comStream = StreamAsIStream.IStreamFrom(stream);\n\n                // does this addref the stream?\n                HRESULT.Check(UnsafeNativeMethods.WICBitmapEncoder.Initialize(\n                    encoderHandle,","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapEncoder.cs#L314-L350","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Always add at least one BitmapFrame before Save: encoder.Frames.Add(BitmapFrame.Create(bitmapSource))","Verify the source BitmapSource decoded successfully before frame creation","Check Frames.Count > 0 before calling Save","Handle decode failures upstream so frame addition is never skipped silently"],"exampleFix":"// before\nvar encoder = new PngBitmapEncoder();\nencoder.Save(ms); // Image_NoFrames\n// after\nvar encoder = new PngBitmapEncoder();\nencoder.Frames.Add(BitmapFrame.Create(bitmapSource));\nencoder.Save(ms);","handlingStrategy":"validation","validationCode":"if (encoder.Frames == null || encoder.Frames.Count == 0)\n    encoder.Frames.Add(BitmapFrame.Create(sourceBitmap));\nencoder.Save(stream);","typeGuard":"static bool HasFrames(BitmapEncoder e) => e.Frames != null && e.Frames.Count > 0;","tryCatchPattern":"try { encoder.Save(stream); }\ncatch (NotSupportedException ex) when (IsNoFrames(ex)) {\n    log.Error(\"No frames added before Save\", ex);\n    encoder.Frames.Add(BitmapFrame.Create(fallbackFrame));\n    encoder.Save(stream); }","preventionTips":["Always add a frame immediately after constructing an encoder","Validate the decode of the source before creating the BitmapFrame","Assert Frames.Count > 0 in Save helper wrappers","Handle null/failed image sources before reaching the save path"],"tags":["wpf","frames","save","imaging"],"backgroundTag":"empty-required-field","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}