{"record":{"id":"38df8957a30a57a9","repo":"dotnet/wpf","slug":"image-metadatanotcompatible","errorCode":null,"errorMessage":"Image_MetadataNotCompatible","messagePattern":"Image_MetadataNotCompatible","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapEncoder.cs","lineNumber":190,"sourceCode":"        {\n            get\n            {\n                VerifyAccess();\n                EnsureBuiltIn();\n                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            {","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapEncoder.cs#L172-L208","documentation":"The BitmapEncoder.Metadata setter requires that the metadata's GuidFormat matches the encoder's ContainerFormat; otherwise the metadata would be written into a container that cannot hold it, and InvalidOperationException(Image_MetadataNotCompatible) is thrown before the global-metadata capability check.","triggerScenarios":"Assigning encoder.Metadata = metadata whose GuidFormat differs from the encoder's container format (e.g. attaching a BitmapMetadata built for JPEG to a PngBitmapEncoder).","commonSituations":"Copying metadata across format conversions (re-encoding JPEG to PNG while reusing the original metadata object); constructing BitmapMetadata with the wrong container GUID.","solutions":["Create BitmapMetadata with the container format matching the target encoder (e.g. new BitmapMetadata(\"/app1/ifd\") for JPEG vs PNG GUID-based metadata)","Rebuild metadata for the target format instead of reusing the source metadata object","Compare metadata.GuidFormat to encoder.ContainerFormat before assigning","Clone/translate needed metadata fields manually when converting formats"],"exampleFix":"// before\npngEncoder.Metadata = jpegMetadata; // GuidFormat mismatch\n// after\nvar pngMeta = new BitmapMetadata(MILGuidData.GUID_ContainerFormatPng.ToString());\npngMeta.SetQuery(\"/text\", ...);\npngEncoder.Metadata = pngMeta;","handlingStrategy":"validation","validationCode":"if (metadata.GuidFormat != encoder.ContainerFormat)\n    throw new InvalidOperationException(\"Metadata GuidFormat does not match encoder container format\");\nencoder.Metadata = metadata;","typeGuard":"static bool IsCompatibleMetadata(BitmapEncoder e, BitmapMetadata m) => m.GuidFormat == e.ContainerFormat;","tryCatchPattern":"try { encoder.Metadata = metadata; }\ncatch (InvalidOperationException) when (metadata.GuidFormat != encoder.ContainerFormat) {\n    encoder.Metadata = RebuildMetadataForFormat(metadata, encoder.ContainerFormat); }","preventionTips":["Never reuse a BitmapMetadata object across different container formats","Build metadata with the target format GUID at conversion time","Compare GuidFormat vs ContainerFormat before assignment","Translate needed fields explicitly when converting formats"],"tags":["wpf","metadata","container-format","imaging"],"backgroundTag":"incompatible-source-type","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"}