Imagesharp: Image.Load() will throw ImageFormatException despite the image appearing to contain a proper data chunk

Created on 25 Feb 2020  Â·  3Comments  Â·  Source: SixLabors/ImageSharp

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] 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†

Description

Attempting to download a specific image to stream via an HttpClient and then loading it via Image.Load<Rgba32>() throws the following exception:

SixLabors.ImageSharp.ImageFormatException: PNG Image does not contain a data chunk

This is the first time this has ever occurred in my code, the exact method I have used to download and load images has never thrown this exception for the hundreds or maybe thousands of other images I and other users have used which called this method. I have asked a few friends to attempt to test this as well, and one other person did and did not report any issues, which makes this even stranger.

Steps to Reproduce

// ignore lack of using statements, I omitted them
var url = "https://cdn.discordapp.com/emojis/598551803566489611.png?v=1"
var response = await new HttpClient().GetAsync(url);
var stream = await response.Content.ReadAsStreamAsync();
var image = Image.Load<Rgba32>(stream); // throws on this line

Both downloading the image in a web browser AND writing stream directly to file produce a clean PNG image with what looks to be intact data chunks when opened with a tool like TweakPNG:
image

(I have essentially no knowledge of PNG headers, data chunks, etc, so if something here looks wrong then I do apologize. Take my knowledge with a grain of salt.)

System Configuration

  • ImageSharp version: v1.0.0-unstable0806
  • Other ImageSharp packages and versions: SixLabors.ImageSharp.Drawing v1.0.0-unstable0421
  • Environment (Operating system, version and so on): Visual Studio 2019 16.4.4, Windows 10 x64
  • .NET Framework version: .NET Core 3.0
  • Additional information: †Before opening this issue, I did scan through closed issues and found other reports of similar behavior, however many of them are related to Azure, which this one does not seem to be.
bug png

All 3 comments

See the comment in #1029

https://github.com/SixLabors/ImageSharp/issues/1029#issuecomment-544061230

The decoder is waiting for the end of the stream which remains open. You should be using your stream.

using var stream = await response.Content.ReadAsStreamAsync();

Hello, thank you for the response.
I noted in the comments that I omitted my using statements as I was pasting a simplified version of the code. Here is the actual code as it is used:

using var response = await Http.GetAsync(url);
if (response.StatusCode == HttpStatusCode.NotFound)
{
    return CommandErrorLocalized("emojiparser_notfound");
}

await using var stream = await response.Content.ReadAsStreamAsync();

using var image = Image.Load<Rgba32>(stream);

Ah right.... The HttpClient bit is a red herring. Looks like we're unable to decode that png, I suspect because of the presence of the unhandled bkGD chunk

I'll see if I can get a fix out asap.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

agoretsky picture agoretsky  Â·  4Comments

Sergio0694 picture Sergio0694  Â·  3Comments

marcpabst picture marcpabst  Â·  3Comments

nullpainter picture nullpainter  Â·  3Comments

JimBobSquarePants picture JimBobSquarePants  Â·  3Comments