Skip to content

Loading image from a stream doesn't start at the current position #2259

Closed
@neilgd

Description

@neilgd

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

2.1.3

Other ImageSharp packages and versions

n/a

Environment (Operating system, version and so on)

Windows 11

.NET Framework version

6.0.0

Description

I have a file that consists of two concatenated images (ie one PNG file, and then a second PNG file).

When I load the first image, the position of the stream increases, but the second read reads from the beginning again. So, now matter how times I load an image, it's always the first image in the stream that gets loaded.

Here's my sample code:

using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;


//var config = new Configuration(new PngConfigurationModule());
//config.ReadOrigin = ReadOrigin.Current; // this makes no difference

//first of all, read the stream sequentially

using var stream = new FileStream("red_and_blue.png", FileMode.Open, FileAccess.Read);

using var i = Image.Load(stream);

Console.WriteLine($"Stream position: {stream.Position}");

using var j = Image.Load(stream);
Console.WriteLine($"Stream position: {stream.Position}"); // THIS HASN'T MOVED

using var i2 = i.CloneAs<Rgb24>();
using var j2 = j.CloneAs<Rgb24>();

Console.WriteLine($"First colour: {i2[5, 5]}");

Console.WriteLine($"Second colour: {j2[5, 5]}"); // THIS SHOULD BE DIFFERENT

//instead, copy the rest of the stream

using var reader = new BinaryReader(stream);
var bytes = reader.ReadBytes((int)(stream.Length - stream.Position));
using var k = Image.Load(bytes);

using var k2 = k.CloneAs<Rgb24>();
Console.WriteLine($"Second colour (2nd attempt): {k2[5, 5]}"); 

Steps to Reproduce

See above

Images

The final image is made by running:

copy /b red.png+blue.png red_and_blue.png

blue
red
red_and_blue

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions