{"record":{"id":"02e2d24db65e2d7e","repo":"dotnet/wpf","slug":"image-encodernoglobalmetadata","errorCode":null,"errorMessage":"Image_EncoderNoGlobalMetadata","messagePattern":"Image_EncoderNoGlobalMetadata","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapEncoder.cs","lineNumber":195,"sourceCode":"                EnsureMetadata(true);\n\n                return _metadata;\n            }\n            set\n            {\n                VerifyAccess();\n                EnsureBuiltIn();\n\n                ArgumentNullException.ThrowIfNull(value);\n\n                if (value.GuidFormat != ContainerFormat)\n                {\n                    throw new InvalidOperationException(SR.Image_MetadataNotCompatible);\n                }\n\n                if (!_supportsGlobalMetadata)\n                {\n                    throw new InvalidOperationException(SR.Image_EncoderNoGlobalMetadata);\n                }\n\n                _metadata = value;\n            }\n        }\n\n        /// <summary>\n        /// Set or get the bitmap's global preview\n        /// </summary>\n        public virtual BitmapSource Preview\n        {\n            get\n            {\n                VerifyAccess();\n                EnsureBuiltIn();\n                return _preview;\n            }\n            set","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapEncoder.cs#L177-L213","documentation":"The BitmapEncoder.Metadata setter rejects metadata when the codec does not support global (container-level) metadata (_supportsGlobalMetadata is false), throwing InvalidOperationException(Image_EncoderNoGlobalMetadata). This follows the GuidFormat compatibility check (1185).","triggerScenarios":"Assigning encoder.Metadata on an encoder whose container format lacks global-metadata support (e.g. BmpBitmapEncoder), even with a format-compatible BitmapMetadata.","commonSituations":"Formats like Bmp have no global metadata blocks; generic encoder pipelines that always set Metadata; migrating encoders between formats without capability checks.","solutions":["Verify codec capability before assigning Metadata","Use a format that supports global metadata (Png, Tiff, Jpeg, Wmp)","Attach metadata at the frame level via frame.Metadata where supported","Skip metadata assignment for unsupported formats"],"exampleFix":"// before\nbmpEncoder.Metadata = metadata; // throws\n// after\nvar tiffEncoder = new TiffBitmapEncoder(); // supports global metadata\ntiffEncoder.Metadata = metadata;","handlingStrategy":"type-guard","validationCode":"if (encoder is BmpBitmapEncoder) skipMetadata = true; // no global metadata support","typeGuard":"static bool SupportsGlobalMetadata(BitmapEncoder e) => e is not BmpBitmapEncoder;","tryCatchPattern":"try { encoder.Metadata = metadata; }\ncatch (InvalidOperationException) { log.Info(\"Codec does not support global metadata; skipping\"); }","preventionTips":["Maintain a capability matrix of WPF codecs vs global metadata","Skip metadata for formats like Bmp","Prefer formats with metadata support (Png/Tiff/Jpeg/Wmp) when metadata matters","Use frame-level metadata where the container lacks global support"],"tags":["wpf","metadata","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"}