ImageSharp icon indicating copy to clipboard operation
ImageSharp copied to clipboard

WebP and TIFF encoders produce invalid images when writing to HttpContext.Response.Body

Open csboling opened this issue 1 year ago • 3 comments

Prerequisites

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

ImageSharp version

3.1.5

Other ImageSharp packages and versions

N/A

Environment (Operating system, version and so on)

Windows 10 19044.2604, 64-bit

.NET Framework version

.NET 8

Description

Encoding an Image as WebP to a Microsoft.AspNetCore.Http.HttpContext (a non-seekable Stream) produces an image that cannot be displayed in the browser (Firefox 129.0.2, Chrome 128.0.6613.114) and has a different length and contents from the same Image written to a file. Writing an Image as TIFF in the same way produces a file which cannot be displayed in the Windows image viewer and contains no valid IFDs. All other formats currently supported by ImageSharp produce valid images when written in this way. Tested with both Image<Rgba32> and Image<Rgb24>. I would guess it affects other non-seekable Streams with these formats as well.

Steps to Reproduce

Here is a code sample reproducing the problem. A complete project can be found here: https://github.com/csboling/imagesharp-webp-http

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet(
    "/download.webp",
    async ctx =>
    {
        using (Image<Rgba32> image = new(1, 1))
        {
            ctx.Response.ContentType = "image/webp";
            await image.SaveAsWebpAsync(ctx.Response.Body);
        }
    });

app.Run();

Images

This ZIP contains example 1x1 pixel RGBA32 images encoded to WebP and TIFF to an HttpContext.Response, then downloaded with Chrome (invalid.webp, invalid.tif) as well as images encoded directly to a file (valid.webp, valid.tif).

examples.zip

csboling avatar Sep 06 '24 18:09 csboling

This is odd. We explicitly wrap non seekable streams https://github.com/SixLabors/ImageSharp/blob/release/3.1.x/src/ImageSharp/Formats/ImageEncoder.cs

JimBobSquarePants avatar Sep 10 '24 03:09 JimBobSquarePants

I can reproduce this issue when writing to a Zip Output Stream, examining the webp file in a hex editor shows that the header doesn't contain a length value. Using version 2.1.9, it works fine.

SamsidParty avatar Sep 20 '24 18:09 SamsidParty

Ok. Thanks for investigating. I’m on holiday in Europe currently don’t have access to a laptop just now so will need someone to debug the issue.

JimBobSquarePants avatar Sep 21 '24 07:09 JimBobSquarePants

I've found a few issues with the current ChunkedMemoryStream implementation. Rewriting it now.

JimBobSquarePants avatar Oct 21 '24 12:10 JimBobSquarePants

Fixed via #2828

JimBobSquarePants avatar Nov 19 '24 10:11 JimBobSquarePants