{"record":{"id":"47dc3228740f7723","repo":"dotnet/wpf","slug":"image-originalstreamreadonly","errorCode":null,"errorMessage":"Image_OriginalStreamReadOnly","messagePattern":"Image_OriginalStreamReadOnly","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDecoder.cs","lineNumber":991,"sourceCode":"                        }\n\n                        _isColorContextCached = true;\n                    }\n                }\n\n                return _readOnlycolorContexts;\n            }\n        }\n\n\n        /// <summary>\n        /// Checks whether the underlying source is writable (useful for in-place metadata editing)\n        /// </summary>\n        internal void CheckOriginalWritable()\n        {\n            if (!_isOriginalWritable)\n            {\n                throw new System.InvalidOperationException(SR.Image_OriginalStreamReadOnly);\n            }\n        }\n\n        #endregion\n\n        #region Internal/Private Methods\n\n        internal static SafeMILHandle SetupDecoderFromUriOrStream(\n            Uri uri,\n            Stream stream,\n            BitmapCacheOption cacheOption,\n            out Guid clsId,\n            out bool isOriginalWritable,\n            out Stream uriStream,\n            out UnmanagedMemoryStream unmanagedMemoryStream,\n            out SafeFileHandle safeFilehandle\n            )\n        {","sourceCodeStart":973,"sourceCodeEnd":1009,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDecoder.cs#L973-L1009","documentation":"CreateInPlaceBitmapMetadataWriter requires the decoder's underlying source stream to be writable. CheckOriginalWritable throws InvalidOperationException with Image_OriginalStreamReadOnly when the original stream or file was opened read-only, so in-place metadata editing cannot proceed.","triggerScenarios":"Calling decoder.CreateInPlaceBitmapMetadataWriter() when the decoder was built from a read-only FileStream, a URI with BitmapCreateOptions opened without write access, a MemoryStream that cannot be written, or a format that does not support in-place metadata updates.","commonSituations":"Editing EXIF/ICC metadata of images loaded from resources embedded in the assembly (read-only); files opened with FileAccess.Read; images on read-only media or without write permissions.","solutions":["Open the backing file with FileAccess.ReadWrite / FileShare.Read so the decoder's _isOriginalWritable is true.","Fall back to non-destructive editing: copy the frame through a BitmapFrame + BitmapMetadata into a new file.","Check stream.CanWrite before calling CreateInPlaceBitmapMetadataWriter.","Catch InvalidOperationException and save metadata via re-encoding instead."],"exampleFix":"// before\nvar fs = File.OpenRead(path); // read-only\nvar dec = BitmapDecoder.Create(fs, opts, BitmapCacheOption.OnLoad);\nvar writer = dec.CreateInPlaceBitmapMetadataWriter(); // throws\n// after\nusing var fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);\nvar dec = BitmapDecoder.Create(fs, opts, BitmapCacheOption.Default);\nvar writer = dec.CreateInPlaceBitmapMetadataWriter();","handlingStrategy":"type-guard","validationCode":"static bool CanEditInPlace(string path) =>\n    File.Exists(path) && new FileInfo(path).IsReadOnly == false;","typeGuard":"bool DecoderSupportsInPlaceWrite(BitmapDecoder d) => d.Frames.Count > 0 && d.ToString() != null && d.InternalImagesCanBeWritten; // check CanWrite on the backing stream before calling","tryCatchPattern":"try { var writer = decoder.CreateInPlaceBitmapMetadataWriter(); writer.SetQuery(\"/app1/ifd/exif\", metadata); }\ncatch (InvalidOperationException) { ReEncodeWithMetadata(decoder, outputPath, metadata); }","preventionTips":["Open files with FileAccess.ReadWrite when in-place metadata editing is planned","Check stream.CanWrite before CreateInPlaceBitmapMetadataWriter","Fall back to re-encode-and-save for read-only sources","Copy resources out of embedded streams before editing"],"tags":["wpf","metadata","read-only","stream","invalid-operation"],"backgroundTag":"permission-denied","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"}