dotnet/wpf · error · InvalidOperationException

SR.Image_MetadataInitializationIncomplete

Error message

SR.Image_MetadataInitializationIncomplete

What it means

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.

Solutions

  1. Create BitmapMetadata only with supported format identifiers: "jpeg", "png", "tiff", "gif", or "wdp".
  2. Verify the source frame actually has metadata (frame.Metadata != null and of expected type) before querying.
  3. Re-acquire metadata through the decoder/encoder rather than reusing a partially constructed instance.
  4. Catch InvalidOperationException around query calls and treat the image as having no metadata of that format.

Example fix

// before
var meta = new BitmapMetadata("bogus");
meta.SetQuery("/app1/ifd/exif/{ushort=271}", "Canon"); // throws: no native handle
// after
var meta = new BitmapMetadata("jpeg");
meta.SetQuery("/app1/ifd/exif/{ushort=271}", "Canon");
Defensive patterns

Strategy: validation

Validate before calling

static readonly string[] SupportedFormats = { "jpeg", "png", "tiff", "gif", "wdp" };
bool IsFormatSupported(string format) =>
    SupportedFormats.Contains(format, StringComparer.OrdinalIgnoreCase);

Type guard

static bool HasNativeMetadata(BitmapMetadata meta) => meta != null && frame.Metadata is BitmapMetadata;

Try / catch

try { var v = meta.GetQuery(query); }
catch (InvalidOperationException) { v = null; /* metadata never initialized / unsupported format */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/5471f05d19dda315. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapMetadata.cs:1505

                return _syncObject;
            }
        }

        internal BitmapMetadataBlockWriter BlockWriter
        {
            get
            {
                return _blockWriter;
            }
        }

        private void EnsureBitmapMetadata()
        {
            ReadPreamble();

            if (_metadataHandle == null)
            {
                throw new System.InvalidOperationException(SR.Image_MetadataInitializationIncomplete);
            }
        }

        #endregion

        private const String policy_Author = "System.Author";
        private const String policy_Title = "System.Title";
        private const String policy_Subject = "System.Subject";
        private const String policy_Comment = "System.Comment";
        private const String policy_Keywords = "System.Keywords";
        private const String policy_DateTaken = "System.Photo.DateTaken";
        private const String policy_ApplicationName = "System.ApplicationName";
        private const String policy_Copyright = "System.Copyright";
        private const String policy_CameraManufacturer = "System.Photo.CameraManufacturer";
        private const String policy_CameraModel = "System.Photo.CameraModel";
        private const String policy_Rating = "System.SimpleRating";

        private SafeMILHandle _metadataHandle;

View on GitHub (pinned to 81131a70a4)