SixLabors/ImageSharp · error · NotSupportedException
Cannot read from the stream.
Error message
Cannot read from the stream.
What it means
ImageSharp cannot perform the requested synchronous operation because the stream's CanRead property is false. The library needs to read bytes from the stream to detect the format and decode, so a non-readable stream is rejected immediately with NotSupportedException. This is a guard inside the shared WithSeekableStream helper used by DetectFormat, Identify, and Load.
Solutions
- Open the stream with read access (FileAccess.Read) before passing it to Image APIs.
- Check stream.CanRead and route to a readable source before calling DetectFormat/Identify/Load.
- If you wrote the image to this stream, create a separate readable stream (e.g. MemoryStream copied from the write stream) for reading.
Example fix
// before
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Write))
{
var format = Image.DetectFormat(stream);
}
// after
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var format = Image.DetectFormat(stream);
} Defensive patterns
Strategy: type-guard
Validate before calling
if (stream is null) throw new ArgumentNullException(nameof(stream));
if (!stream.CanRead) throw new ArgumentException("Stream must support reading.", nameof(stream)); Type guard
static bool IsReadable(Stream stream) => stream is { CanRead: true }; Try / catch
try { var format = Image.DetectFormat(stream); }
catch (NotSupportedException ex) { /* use a readable stream instead */ } Prevention
- Open file streams with FileAccess.Read for decoding operations.
- Never pass response/output streams to read APIs; use request body or a fresh file stream.
- Wrap streams carefully and avoid reading from disposed streams (ObjectDisposedException may surface as CanRead == false).
When it happens
Trigger: Passing a stream opened for write-only access (e.g. new FileStream(path, FileMode.Open, FileAccess.Write)) or a disposed/closed stream to Image.DetectFormat, Image.Identify, or Image.Load.
Common situations: Accidentally reusing the response/output stream for reading (e.g. HttpContext.Response.Body in ASP.NET), passing a stream returned from a write-only API, or reading from a stream already disposed.
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 write to the stream.
- The embedded ANI frame resource contains an invalid seek…
- Must be bytes. Was bytes.
- Unexpected end of stream while reading gif application…
- The embedded icon resource contains an invalid seek offset.
AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13).
Data as JSON: /api/errors/c65a1b24ce65fdf0.
Report an issue: GitHub.
Appendix: source
Thrown at src/ImageSharp/Image.FromStream.cs:290
/// Performs the given action against the stream ensuring that it is seekable.
/// </summary>
/// <typeparam name="T">The type of object returned from the action.</typeparam>
/// <param name="options">The general decoder options.</param>
/// <param name="stream">The input stream.</param>
/// <param name="action">The action to perform.</param>
/// <returns>The <typeparamref name="T"/>.</returns>
/// <exception cref="NotSupportedException">Cannot read from the stream.</exception>
internal static T WithSeekableStream<T>(
DecoderOptions options,
Stream stream,
Func<Stream, T> action)
{
Guard.NotNull(options, nameof(options));
Guard.NotNull(stream, nameof(stream));
if (!stream.CanRead)
{
throw new NotSupportedException("Cannot read from the stream.");
}
Configuration configuration = options.Configuration;
if (stream.CanSeek)
{
if (configuration.ReadOrigin == ReadOrigin.Begin)
{
stream.Position = 0;
}
return action(stream);
}
using ChunkedMemoryStream memoryStream = new(configuration.MemoryAllocator);
stream.CopyTo(memoryStream, configuration.StreamProcessingBufferSize);
memoryStream.Position = 0;
return action(memoryStream);View on GitHub (pinned to 59ce6af6fc)