{"record":{"id":"c65a1b24ce65fdf0","repo":"SixLabors/ImageSharp","slug":"cannot-read-from-the-stream-image-fromstream","errorCode":null,"errorMessage":"Cannot read from the stream.","messagePattern":"Cannot read from the stream\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Image.FromStream.cs","lineNumber":290,"sourceCode":"    /// Performs the given action against the stream ensuring that it is seekable.\n    /// </summary>\n    /// <typeparam name=\"T\">The type of object returned from the action.</typeparam>\n    /// <param name=\"options\">The general decoder options.</param>\n    /// <param name=\"stream\">The input stream.</param>\n    /// <param name=\"action\">The action to perform.</param>\n    /// <returns>The <typeparamref name=\"T\"/>.</returns>\n    /// <exception cref=\"NotSupportedException\">Cannot read from the stream.</exception>\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        Configuration configuration = options.Configuration;\n        if (stream.CanSeek)\n        {\n            if (configuration.ReadOrigin == ReadOrigin.Begin)\n            {\n                stream.Position = 0;\n            }\n\n            return action(stream);\n        }\n\n        using ChunkedMemoryStream memoryStream = new(configuration.MemoryAllocator);\n        stream.CopyTo(memoryStream, configuration.StreamProcessingBufferSize);\n        memoryStream.Position = 0;\n\n        return action(memoryStream);","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Image.FromStream.cs#L272-L308","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nusing (var stream = new FileStream(path, FileMode.Open, FileAccess.Write))\n{\n    var format = Image.DetectFormat(stream);\n}\n// after\nusing (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))\n{\n    var format = Image.DetectFormat(stream);\n}","handlingStrategy":"type-guard","validationCode":"if (stream is null) throw new ArgumentNullException(nameof(stream));\nif (!stream.CanRead) throw new ArgumentException(\"Stream must support reading.\", nameof(stream));","typeGuard":"static bool IsReadable(Stream stream) => stream is { CanRead: true };","tryCatchPattern":"try { var format = Image.DetectFormat(stream); }\ncatch (NotSupportedException ex) { /* use a readable stream instead */ }","preventionTips":["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)."],"tags":["stream","io","argument-validation"],"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"}