{"record":{"id":"141807a100fe0eb0","repo":"SixLabors/ImageSharp","slug":"cannot-write-to-the-stream","errorCode":null,"errorMessage":"Cannot write to the stream.","messagePattern":"Cannot write to the stream\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/ImageExtensions.cs","lineNumber":94,"sourceCode":"\n    /// <summary>\n    /// Writes the image to the given stream using the given image format.\n    /// </summary>\n    /// <param name=\"source\">The source image.</param>\n    /// <param name=\"stream\">The stream to save the image to.</param>\n    /// <param name=\"format\">The format to save the image in.</param>\n    /// <exception cref=\"ArgumentNullException\">The stream is null.</exception>\n    /// <exception cref=\"ArgumentNullException\">The format is null.</exception>\n    /// <exception cref=\"NotSupportedException\">The stream is not writable.</exception>\n    /// <exception cref=\"NotSupportedException\">No encoder available for provided format.</exception>\n    public static void Save(this Image source, Stream stream, IImageFormat format)\n    {\n        Guard.NotNull(stream, nameof(stream));\n        Guard.NotNull(format, nameof(format));\n\n        if (!stream.CanWrite)\n        {\n            throw new NotSupportedException(\"Cannot write to the stream.\");\n        }\n\n        IImageEncoder encoder = source.Configuration.ImageFormatsManager.GetEncoder(format);\n\n        if (encoder is null)\n        {\n            StringBuilder sb = new();\n            sb.AppendLine(\"No encoder was found for the provided mime type. Registered encoders include:\");\n\n            foreach (KeyValuePair<IImageFormat, IImageEncoder> val in source.Configuration.ImageFormatsManager.ImageEncoders)\n            {\n                sb.AppendFormat(CultureInfo.InvariantCulture, \" - {0} : {1}{2}\", val.Key.Name, val.Value.GetType().Name, Environment.NewLine);\n            }\n\n            throw new NotSupportedException(sb.ToString());\n        }\n\n        source.Save(stream, encoder);","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/ImageExtensions.cs#L76-L112","documentation":"ImageExtensions.Save cannot write the encoded image because the target stream's CanWrite property is false. Encoding requires writing bytes to the stream, so a non-writable stream is rejected immediately with NotSupportedException before the encoder runs.","triggerScenarios":"Calling image.Save(stream, format) with a read-only stream (e.g. FileStream opened with FileAccess.Read) or a disposed stream.","commonSituations":"Passing a file opened for reading when you meant to open it for writing, saving to the request body stream of a server, or reusing a stream that was already closed.","solutions":["Open the destination stream with write access (FileAccess.Write or FileMode.Create).","Check stream.CanWrite before calling Save.","If you need the image in a read-only stream's backing store, write to a new stream bound to that resource instead."],"exampleFix":"// before\nusing (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))\n{\n    image.Save(stream, format);\n}\n// after\nusing (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))\n{\n    image.Save(stream, format);\n}","handlingStrategy":"type-guard","validationCode":"if (!stream.CanWrite) throw new ArgumentException(\"Stream must support writing.\", nameof(stream));","typeGuard":"static bool IsWritable(Stream stream) => stream is { CanWrite: true };","tryCatchPattern":"try { image.Save(stream, format); }\ncatch (NotSupportedException ex) { /* switch to a writable destination stream */ }","preventionTips":["Open destination files with FileMode.Create and FileAccess.Write.","Never save to request input streams; use response/body output streams instead.","Check CanWrite when streams come from external code or abstractions."],"tags":["stream","io","save"],"backgroundTag":"incompatible-source-type","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}