{"record":{"id":"d51ce05eacc4e8b6","repo":"dotnet/wpf","slug":"sr-image-metadatareadonly","errorCode":null,"errorMessage":"SR.Image_MetadataReadOnly","messagePattern":"SR\\.Image_MetadataReadOnly","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapMetadata.cs","lineNumber":1018,"sourceCode":"\n        #endregion\n\n        #region Query Methods\n\n        /// <summary>\n        ///\n        /// </summary>\n        public void SetQuery(String query, object value)\n        {\n            WritePreamble();\n\n            ArgumentNullException.ThrowIfNull(query);\n\n            ArgumentNullException.ThrowIfNull(value);\n\n            if (_readOnly)\n            {\n                throw new System.InvalidOperationException(SR.Image_MetadataReadOnly);\n            }\n\n            // Store these for debugging stress failures.\n            _setQueryString = query;\n            _setQueryValue = value;\n\n            EnsureBitmapMetadata();\n\n            PROPVARIANT propVar = new PROPVARIANT();\n\n            try\n            {\n                propVar.Init(value);\n\n                if (propVar.RequiresSyncObject)\n                {\n                    BitmapMetadata metadata = value as BitmapMetadata;\n                    Invariant.Assert(metadata != null);","sourceCodeStart":1000,"sourceCodeEnd":1036,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapMetadata.cs#L1000-L1036","documentation":"Thrown from BitmapMetadata.SetQuery (and similar write paths) when the metadata object was opened in read-only mode (_readOnly == true). WPF marks metadata read-only when it comes from a read-only source, such as a BitmapFrame decoded from a stream not opened for writing or metadata obtained from a frozen frame. Writing a query to such metadata is an invalid operation, so the library refuses it up front.","triggerScenarios":"Calling BitmapMetadata.SetQuery(query, value) on a BitmapMetadata whose _readOnly flag is set — typically metadata retrieved from a frame of an image decoded without write access (e.g., BitmapCreateOptions or reading from a non-writable stream), or metadata from a frozen bitmap.","commonSituations":"Trying to embed EXIF/IPTC/XMP tags into a JPEG loaded from a web stream or Resource; reading metadata with a decoder and then attempting SetQuery on it; sharing one BitmapMetadata between a read-only frame and an intended-write encoder.","solutions":["Obtain a writable BitmapMetadata: decode with write access (e.g., create metadata via new BitmapMetadata(\"jpeg\") and set it on the encoder) rather than mutating decoder-supplied metadata.","Check IsReadOnly (or IsFrozen) before calling SetQuery and clone the metadata into a new writable BitmapMetadata if needed.","Use BitmapEncoder with InPlaceBitmapMetadataWriter for in-place edits on file streams opened for writing.","Wrap SetQuery in try/catch for InvalidOperationException and surface that the metadata source is read-only."],"exampleFix":"// before\nvar meta = frame.Metadata as BitmapMetadata;\nmeta.SetQuery(\"/app1/ifd/exif/{ushort=271}\", \"Canon\"); // throws: read-only\n// after\nvar meta = new BitmapMetadata(\"jpeg\");\nmeta.SetQuery(\"/app1/ifd/exif/{ushort=271}\", \"Canon\");\nencoder.Frames.Add(BitmapFrame.Create(source, frame.Thumbnail, meta, frame.ColorContexts));","handlingStrategy":"validation","validationCode":"if (meta == null) throw new ArgumentNullException(nameof(meta));\nif (meta.IsReadOnly || meta.IsFrozen)\n    meta = new BitmapMetadata(\"jpeg\") { /* copy fields */ };","typeGuard":"static bool IsWritable(BitmapMetadata meta) => meta != null && !meta.IsReadOnly && !meta.IsFrozen;","tryCatchPattern":"try { meta.SetQuery(query, value); }\ncatch (InvalidOperationException) { meta = CloneWritable(meta); meta.SetQuery(query, value); }","preventionTips":["Check IsReadOnly before every SetQuery call.","Treat decoder-supplied metadata as read-only; create a new BitmapMetadata for encoder output.","Use InPlaceBitmapMetadataWriter for file edits instead of mutating decoded metadata.","Don't share one BitmapMetadata between a read-only frame and an encoder."],"tags":["wpf","bitmapmetadata","read-only","setquery","invalidoperation"],"backgroundTag":"unsupported-operation","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"}