dotnet/wpf · error · NotSupportedException

Image_MetadataNotSupported

Error message

Image_MetadataNotSupported

What it means

BitmapImage overrides Metadata with a getter that always throws NotSupportedException with SR.Image_MetadataNotSupported. BitmapImage does not expose decoder metadata by design; to read metadata you must use BitmapFrame/BitmapDecoder instead.

Solutions

  1. Get metadata from the decoder instead: BitmapDecoder.Create(image.UriSource, ...).Frames[0].Metadata.
  2. Cast/check for BitmapFrame first: if (source is BitmapFrame f) use f.Metadata.
  3. Wrap the access in try/catch (NotSupportedException) when metadata is optional.
  4. Restructure loading code to keep a reference to the BitmapDecoder/BitmapFrame rather than only the BitmapImage.

Example fix

// before
var meta = ((BitmapImage)img.Source).Metadata; // NotSupported

// after
var dec = BitmapDecoder.Create(((BitmapImage)img.Source).UriSource, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
var meta = dec.Frames[0].Metadata;
Defensive patterns

Strategy: try-catch

Validate before calling

if (source is BitmapImage) { /* metadata not available; resolve via BitmapDecoder */ }

Type guard

bool hasMetadata(ImageSource src) => src is BitmapFrame or BitmapDecoder;

Try / catch

try { var meta = bitmapImage.Metadata; }
catch (NotSupportedException) { var meta = BitmapDecoder.Create(bitmapImage.UriSource, BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames[0].Metadata; }

Prevention

When it happens

Trigger: Reading the Metadata property on any BitmapImage instance, e.g. ((BitmapImage)image.Source).Metadata.

Common situations: Code that reads ImageSource.Metadata generically after loading via BitmapImage; porting code from BitmapFrame to BitmapImage and expecting metadata access to still work.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapImage.cs:140

        /// Returns if the BitmapFrame is downloading content
        /// </summary>
        public override bool IsDownloading
        {
            get
            {
                ReadPreamble();
                return _isDownloading;
            }
        }

        /// <summary>
        /// Get the Metadata of the bitmap
        /// </summary>
        public override ImageMetadata Metadata
        {
            get
            {
                throw new System.NotSupportedException(SR.Image_MetadataNotSupported);
            }
        }


        #endregion

        #region ToString

        /// <summary>
        /// Can serialze "this" to a string
        /// </summary>
        internal override bool CanSerializeToString()
        {
            ReadPreamble();

            return (
                // UriSource not null
                UriSource != null &&

View on GitHub (pinned to 81131a70a4)