{"record":{"id":"b4c31a1a1e4499f8","repo":"dotnet/wpf","slug":"image-encodernopreview","errorCode":null,"errorMessage":"Image_EncoderNoPreview","messagePattern":"Image_EncoderNoPreview","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapEncoder.cs","lineNumber":222,"sourceCode":"        /// </summary>\n        public virtual BitmapSource Preview\n        {\n            get\n            {\n                VerifyAccess();\n                EnsureBuiltIn();\n                return _preview;\n            }\n            set\n            {\n                VerifyAccess();\n                EnsureBuiltIn();\n\n                ArgumentNullException.ThrowIfNull(value);\n\n                if (!_supportsPreview)\n                {\n                    throw new InvalidOperationException(SR.Image_EncoderNoPreview);\n                }\n\n                _preview = value;\n            }\n        }\n\n        /// <summary>\n        /// The info that identifies this codec.\n        /// </summary>\n        public virtual BitmapCodecInfo CodecInfo\n        {\n            get\n            {\n                VerifyAccess();\n                EnsureBuiltIn();\n                EnsureUnmanagedEncoder();\n\n                // There should always be a codec info.","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapEncoder.cs#L204-L240","documentation":"The BitmapEncoder.Preview setter rejects a preview image when the codec does not support a container-level preview (_supportsPreview is false), throwing InvalidOperationException(Image_EncoderNoPreview). Previews are only supported by codecs that can embed them (e.g. HD Photo/WMP).","triggerScenarios":"Assigning encoder.Preview = someBitmapSource on an encoder whose codec lacks preview support (checked in the setter after EnsureBuiltIn and null checks).","commonSituations":"Trying to embed previews in Png/Jpeg/Tiff encoders, which do not support global previews; code copied from WMP/HD Photo workflows.","solutions":["Only set Preview on codecs that support it (e.g. WmpBitmapEncoder)","Remove the Preview assignment for other formats","Use the frame Thumbnail support instead where the codec allows it","Gate the assignment on codec capability info"],"exampleFix":"// before\npngEncoder.Preview = preview; // throws\n// after\nif (encoder is WmpBitmapEncoder wmp) { wmp.Preview = preview; }","handlingStrategy":"type-guard","validationCode":"if (encoder is not WmpBitmapEncoder) preview = null; // only WMP supports container previews","typeGuard":"static bool SupportsPreview(BitmapEncoder e) => e is WmpBitmapEncoder;","tryCatchPattern":"try { encoder.Preview = previewImage; }\ncatch (InvalidOperationException) { /* preview unsupported by codec; drop it */ }","preventionTips":["Only embed previews with WmpBitmapEncoder/HD Photo","Do not port preview code across encoder types","Remove Preview assignment for Png/Jpeg/Tiff/Bmp targets","Document codec capability differences in imaging helper APIs"],"tags":["wpf","preview","invalid-operation","imaging"],"backgroundTag":"unsupported-operation","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"}