{"record":{"id":"27f2e6b07ef39cd6","repo":"SixLabors/ImageSharp","slug":"cannot-read-from-the-stream","errorCode":null,"errorMessage":"Cannot read from the stream.","messagePattern":"Cannot read from the stream\\.","errorType":"validation","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/ImageDecoder.cs","lineNumber":196,"sourceCode":"            return false;\n        }\n\n        Size targetSize = options.TargetSize.Value;\n        Size currentSize = image.Size;\n        return currentSize.Width != targetSize.Width && currentSize.Height != targetSize.Height;\n    }\n\n    internal static T WithSeekableStream<T>(\n        DecoderOptions options,\n        Stream stream,\n        Func<Stream, T> action)\n    {\n        Guard.NotNull(options, nameof(options));\n        Guard.NotNull(stream, nameof(stream));\n\n        if (!stream.CanRead)\n        {\n            throw new NotSupportedException(\"Cannot read from the stream.\");\n        }\n\n        T PerformActionAndResetPosition(Stream s, long position)\n        {\n            T result = action(s);\n\n            // Issue #2259. Our buffered reads may have left the stream in an incorrect non-zero position.\n            // Reset the position of the seekable stream if we did not read to the end to allow additional reads.\n            // The stream is always seekable in this scenario.\n            if (stream.Position != s.Position && s.Position != s.Length)\n            {\n                stream.Position = position + s.Position;\n            }\n\n            return result;\n        }\n\n        if (stream.CanSeek)","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/ImageDecoder.cs#L178-L214","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Open the source stream with read access (FileAccess.Read / FileMode.Open) before passing it to Load/Identify","Check stream.CanRead before calling the decoder API and surface a clear error early","Ensure the stream is not disposed before decoding completes — keep it alive inside the using scope of the Load call","If wrapping another stream, forward CanRead correctly instead of returning false"],"exampleFix":"// before\nusing var fs = new FileStream(path, FileMode.Append, FileAccess.Write);\nvar image = Image.Load(fs); // NotSupportedException\n// after\nusing var fs = new FileStream(path, FileMode.Open, FileAccess.Read);\nvar image = Image.Load(fs);","handlingStrategy":"validation","validationCode":"if (stream is null) throw new ArgumentNullException(nameof(stream));\nif (!stream.CanRead) throw new InvalidOperationException(\"Stream must be readable before decoding.\");\nvar image = Image.Load(stream);","typeGuard":"static bool IsReadable(Stream? s) => s is { CanRead: true };","tryCatchPattern":null,"preventionTips":["Always open files with File.OpenRead / FileAccess.Read for decoding","Never dispose a stream before the Load/Identify call returns","Assert stream.CanRead in debug builds before decode calls"],"tags":["dotnet","image-decoding","stream","argument"],"backgroundTag":"unsupported-operation","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"}