dotnet/wpf · error · InvalidOperationException

SR.Image_InplaceMetadataNoCopy

Error message

SR.Image_InplaceMetadataNoCopy

What it means

InplaceBitmapMetadataWriter writes metadata in place inside an existing image file/frame and cannot be duplicated. Freezable.CreateInstanceCore is part of Freezable's clone machinery, which would require copying the writer, so WPF throws this InvalidOperationException by design.

Solutions

  1. Do not clone InplaceBitmapMetadataWriter; perform metadata edits directly on the single in-place instance.
  2. To keep a copy of metadata, read the values (e.g. via GetQuery) and store them yourself, then create a new BitmapMetadata object for the copy.
  3. Use BitmapMetadata obtained from a frame (frame.Metadata) for non-inplace scenarios instead of the inplace writer.
Defensive patterns

Strategy: type-guard

Validate before calling

if (freezable is InplaceBitmapMetadataWriter) { /* do not clone; edit in place */ }

Type guard

static bool IsClonableMetadata(Freezable f) => f is not InplaceBitmapMetadataWriter;

Try / catch

try { var copy = f.Clone(); } catch (InvalidOperationException ex) { /* fall back to reading GetQuery values */ }

Prevention

When it happens

Trigger: Any code path that invokes Freezable.CreateInstanceCore on an InplaceBitmapMetadataWriter — e.g. calling Clone(), CloneCurrentValue(), GetAsFrozen() or other Freezable copy operations that internally default-construct a new instance.

Common situations: Developers treating the metadata writer like a normal Freezable bitmap object and calling Clone() to snapshot metadata before editing; WPF data-binding or animation infrastructure that automatically clones Freezable values.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/InplaceBitmapMetadataWriter.cs:133

        #region Freezable

        /// <summary>
        ///     Shadows inherited Copy() with a strongly typed
        ///     version for convenience.
        /// </summary>
        public new InPlaceBitmapMetadataWriter Clone()
        {
            return (InPlaceBitmapMetadataWriter)base.Clone();
        }

        /// <summary>
        /// Implementation of <see cref="System.Windows.Freezable.CreateInstanceCore">Freezable.CreateInstanceCore</see>.
        /// </summary>
        /// <returns>The new Freezable.</returns>
        protected override Freezable CreateInstanceCore()
        {
            throw new InvalidOperationException(SR.Image_InplaceMetadataNoCopy);
        }

        /// <summary>
        /// Implementation of <see cref="System.Windows.Freezable.CloneCore(Freezable)">Freezable.CloneCore</see>.
        /// </summary>
        protected override void CloneCore(Freezable sourceFreezable)
        {
            throw new InvalidOperationException(SR.Image_InplaceMetadataNoCopy);
        }

        /// <summary>
        /// Implementation of <see cref="System.Windows.Freezable.CloneCurrentValueCore(Freezable)">Freezable.CloneCurrentValueCore</see>.
        /// </summary>
        protected override void CloneCurrentValueCore(Freezable sourceFreezable)
        {
            throw new InvalidOperationException(SR.Image_InplaceMetadataNoCopy);
        }

View on GitHub (pinned to 81131a70a4)