{"record":{"id":"5471f05d19dda315","repo":"dotnet/wpf","slug":"sr-image-metadatainitializationincomplete","errorCode":null,"errorMessage":"SR.Image_MetadataInitializationIncomplete","messagePattern":"SR\\.Image_MetadataInitializationIncomplete","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapMetadata.cs","lineNumber":1505,"sourceCode":"                return _syncObject;\n            }\n        }\n\n        internal BitmapMetadataBlockWriter BlockWriter\n        {\n            get\n            {\n                return _blockWriter;\n            }\n        }\n\n        private void EnsureBitmapMetadata()\n        {\n            ReadPreamble();\n\n            if (_metadataHandle == null)\n            {\n                throw new System.InvalidOperationException(SR.Image_MetadataInitializationIncomplete);\n            }\n        }\n\n        #endregion\n\n        private const String policy_Author = \"System.Author\";\n        private const String policy_Title = \"System.Title\";\n        private const String policy_Subject = \"System.Subject\";\n        private const String policy_Comment = \"System.Comment\";\n        private const String policy_Keywords = \"System.Keywords\";\n        private const String policy_DateTaken = \"System.Photo.DateTaken\";\n        private const String policy_ApplicationName = \"System.ApplicationName\";\n        private const String policy_Copyright = \"System.Copyright\";\n        private const String policy_CameraManufacturer = \"System.Photo.CameraManufacturer\";\n        private const String policy_CameraModel = \"System.Photo.CameraModel\";\n        private const String policy_Rating = \"System.SimpleRating\";\n\n        private SafeMILHandle _metadataHandle;","sourceCodeStart":1487,"sourceCodeEnd":1523,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapMetadata.cs#L1487-L1523","documentation":"EnsureBitmapMetadata() throws this InvalidOperationException when the BitmapMetadata's native (_metadataHandle) has not been created. The managed wrapper exists but the underlying WIC metadata object was never initialized, so any query operation would access a null native handle; WPF throws instead to avoid an internal null dereference.","triggerScenarios":"Calling GetQuery/SetQuery/RemoveQuery (or any path that calls EnsureBitmapMetadata) on a BitmapMetadata whose native metadata block was never created — e.g., a metadata object created with an invalid or null content type, or one that failed native initialization, or metadata from a frame with no metadata blocks of that format.","commonSituations":"Constructed BitmapMetadata with an unsupported/empty format string (e.g., not \"jpeg\", \"png\", \"tiff\", \"gif\", \"wdp\"); decoder returned a frame without the expected metadata block; calling GetQuery on a metadata object before the encoder/decoder pipeline attached native resources.","solutions":["Create BitmapMetadata only with supported format identifiers: \"jpeg\", \"png\", \"tiff\", \"gif\", or \"wdp\".","Verify the source frame actually has metadata (frame.Metadata != null and of expected type) before querying.","Re-acquire metadata through the decoder/encoder rather than reusing a partially constructed instance.","Catch InvalidOperationException around query calls and treat the image as having no metadata of that format."],"exampleFix":"// before\nvar meta = new BitmapMetadata(\"bogus\");\nmeta.SetQuery(\"/app1/ifd/exif/{ushort=271}\", \"Canon\"); // throws: no native handle\n// after\nvar meta = new BitmapMetadata(\"jpeg\");\nmeta.SetQuery(\"/app1/ifd/exif/{ushort=271}\", \"Canon\");","handlingStrategy":"validation","validationCode":"static readonly string[] SupportedFormats = { \"jpeg\", \"png\", \"tiff\", \"gif\", \"wdp\" };\nbool IsFormatSupported(string format) =>\n    SupportedFormats.Contains(format, StringComparer.OrdinalIgnoreCase);","typeGuard":"static bool HasNativeMetadata(BitmapMetadata meta) => meta != null && frame.Metadata is BitmapMetadata;","tryCatchPattern":"try { var v = meta.GetQuery(query); }\ncatch (InvalidOperationException) { v = null; /* metadata never initialized / unsupported format */ }","preventionTips":["Only construct BitmapMetadata with supported format strings (jpeg, png, tiff, gif, wdp).","Check frame.Metadata for null before casting to BitmapMetadata.","Prefer metadata obtained from decoder/encoder frames over hand-constructed instances.","Catch InvalidOperationException on query access and treat as 'no metadata' rather than crashing."],"tags":["wpf","bitmapmetadata","uninitialized","native-handle","invalidoperation"],"backgroundTag":"module-init-failed","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"}