SixLabors/ImageSharp · error · NotSupportedException
Cannot write to the stream.
Error message
Cannot write to the stream.
What it means
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.
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.
Example fix
// before
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
image.Save(stream, format);
}
// after
using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
image.Save(stream, format);
} Defensive patterns
Strategy: type-guard
Validate before calling
if (!stream.CanWrite) throw new ArgumentException("Stream must support writing.", nameof(stream)); Type guard
static bool IsWritable(Stream stream) => stream is { CanWrite: true }; Try / catch
try { image.Save(stream, format); }
catch (NotSupportedException ex) { /* switch to a writable destination stream */ } Prevention
- 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.
When it happens
Trigger: Calling image.Save(stream, format) with a read-only stream (e.g. FileStream opened with FileAccess.Read) or a disposed stream.
Common situations: 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.
Understand the failure class
Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.
Related errors
- Cannot read from the stream.
- The embedded ANI frame resource contains an invalid seek…
- Unexpected end of stream while reading gif application…
- The embedded icon resource contains an invalid seek offset.
- Cannot read from the stream.
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/141807a100fe0eb0.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/ImageExtensions.cs:94
/// <summary>
/// Writes the image to the given stream using the given image format.
/// </summary>
/// <param name="source">The source image.</param>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="format">The format to save the image in.</param>
/// <exception cref="ArgumentNullException">The stream is null.</exception>
/// <exception cref="ArgumentNullException">The format is null.</exception>
/// <exception cref="NotSupportedException">The stream is not writable.</exception>
/// <exception cref="NotSupportedException">No encoder available for provided format.</exception>
public static void Save(this Image source, Stream stream, IImageFormat format)
{
Guard.NotNull(stream, nameof(stream));
Guard.NotNull(format, nameof(format));
if (!stream.CanWrite)
{
throw new NotSupportedException("Cannot write to the stream.");
}
IImageEncoder encoder = source.Configuration.ImageFormatsManager.GetEncoder(format);
if (encoder is null)
{
StringBuilder sb = new();
sb.AppendLine("No encoder was found for the provided mime type. Registered encoders include:");
foreach (KeyValuePair<IImageFormat, IImageEncoder> val in source.Configuration.ImageFormatsManager.ImageEncoders)
{
sb.AppendFormat(CultureInfo.InvariantCulture, " - {0} : {1}{2}", val.Key.Name, val.Value.GetType().Name, Environment.NewLine);
}
throw new NotSupportedException(sb.ToString());
}
source.Save(stream, encoder);View on GitHub (pinned to 59ce6af6fc)