SixLabors/ImageSharp · error · NotSupportedException

Cannot read from the stream.

Error message

Cannot read from the stream.

What it means

ImageSharp's decoder pipeline wraps the input stream in WithSeekableStream, which first verifies the stream is readable. If stream.CanRead is false it throws NotSupportedException, because decoding cannot proceed at all on a closed, write-only, or otherwise unreadable stream.

Solutions

  1. Open the source stream with read access (FileAccess.Read / FileMode.Open) before passing it to Load/Identify
  2. Check stream.CanRead before calling the decoder API and surface a clear error early
  3. Ensure the stream is not disposed before decoding completes — keep it alive inside the using scope of the Load call
  4. If wrapping another stream, forward CanRead correctly instead of returning false

Example fix

// before
using var fs = new FileStream(path, FileMode.Append, FileAccess.Write);
var image = Image.Load(fs); // NotSupportedException
// after
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
var image = Image.Load(fs);
Defensive patterns

Strategy: validation

Validate before calling

if (stream is null) throw new ArgumentNullException(nameof(stream));
if (!stream.CanRead) throw new InvalidOperationException("Stream must be readable before decoding.");
var image = Image.Load(stream);

Type guard

static bool IsReadable(Stream? s) => s is { CanRead: true };

Prevention

When it happens

Trigger: Calling Image.Load/LoadAsync, Image.Identify/IdentifyAsync, or any ImageDecoder.Decode/Identify overload with a stream whose CanRead is false — e.g. a disposed FileStream/MemoryStream, a stream opened for writing only, or a FileStream with FileAccess.Write.

Common situations: Using a stream after it has been disposed (e.g. after a using block ended); creating a FileStream with FileMode.Append/FileAccess.Write and passing it to a decoder; passing a response stream that has been closed by an HTTP layer; passing Stream.Null in code paths where it was constructed with write access.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/27f2e6b07ef39cd6. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Formats/ImageDecoder.cs:196

            return false;
        }

        Size targetSize = options.TargetSize.Value;
        Size currentSize = image.Size;
        return currentSize.Width != targetSize.Width && currentSize.Height != targetSize.Height;
    }

    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.");
        }

        T PerformActionAndResetPosition(Stream s, long position)
        {
            T result = action(s);

            // Issue #2259. Our buffered reads may have left the stream in an incorrect non-zero position.
            // Reset the position of the seekable stream if we did not read to the end to allow additional reads.
            // The stream is always seekable in this scenario.
            if (stream.Position != s.Position && s.Position != s.Length)
            {
                stream.Position = position + s.Position;
            }

            return result;
        }

        if (stream.CanSeek)

View on GitHub (pinned to 59ce6af6fc)