Browse Source

Add test and fix for asynchronous streams.

pull/2267/head
James Jackson-South 3 years ago
parent
commit
bf07e4507c
  1. 15
      src/ImageSharp/Image.FromStream.cs
  2. 15
      tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

15
src/ImageSharp/Image.FromStream.cs

@ -588,7 +588,20 @@ public abstract partial class Image
await stream.CopyToAsync(memoryStream, configuration.StreamProcessingBufferSize, cancellationToken).ConfigureAwait(false);
memoryStream.Position = 0;
return action(memoryStream, cancellationToken);
T Action(Stream ms, CancellationToken ct)
{
// Reset the position of the seekable stream if we did not read to the end
// to allow additional reads.
T result = action(ms, ct);
if (stream.CanSeek && ms.Position != ms.Length)
{
stream.Position = ms.Position;
}
return result;
}
return Action(memoryStream, cancellationToken);
}
[DoesNotReturn]

15
tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

@ -61,6 +61,21 @@ public class GeneralFormatTests
Assert.NotEqual(i[5, 5], j[5, 5]);
}
[Fact]
public async Task ReadOriginIsRespectedOnLoadAsync()
{
using FileStream stream = File.OpenRead(TestFile.GetInputFileFullPath(TestImages.Png.Issue2259));
using Image<Rgb24> i = await Image.LoadAsync<Rgb24>(stream);
long position1 = stream.Position;
Assert.NotEqual(0, position1);
using Image<Rgb24> j = await Image.LoadAsync<Rgb24>(stream);
long position2 = stream.Position;
Assert.True(position2 > position1);
Assert.NotEqual(i[5, 5], j[5, 5]);
}
[Fact]
public void ImageCanEncodeToString()
{

Loading…
Cancel
Save