Imagesharp: SixLabors.ImageSharp.UnknownImageFormatException: Image cannot be loaded

Created on 28 Oct 2020  路  6Comments  路  Source: SixLabors/ImageSharp

Prerequisites

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

Description

Trying to load the JPG image below throws the following
images

SixLabors.ImageSharp.UnknownImageFormatException: Image cannot be loaded. Available decoders:
 - TGA : TgaDecoder
 - PNG : PngDecoder
 - BMP : BmpDecoder
 - JPEG : JpegDecoder
 - GIF : GifDecoder

Steps to Reproduce

The sample code

using (Image image = await Image.LoadAsync(imageStream))
{
    PngEncoder pngEncoder = new PngEncoder();
    pngEncoder.CompressionLevel = PngCompressionLevel.BestCompression;
    await image.SaveAsync(imageStream, pngEncoder);
    imageStream.Seek(0, SeekOrigin.Begin);
}

System Configuration

  • ImageSharp version: 1.0.1
  • Environment (Operating system, version and so on):

    • Windows 10 Pro - Version 1903 - Build 18362.1139

  • .NET Core version: 2.1
invalid

All 6 comments

Unless you have reset the position of the input stream before the code sample you posted then it looks like your input stream is in the wrong position. We do not reset that position for you on load unless explicitly instructed to do so.

Here's the result of loading the image and saving it as a png.

1406

Hey @JimBobSquarePants

Thanks for the tip, I'll try it out.

Cheers.

Still not working. I suspect Github altered the original image.
Can you try again, please?
https://we.tl/t-85LLGWncqZ

using (Image image = await Image.LoadAsync(GetImageSharpConfiguration(), imageStream))
{
    PngEncoder pngEncoder = new PngEncoder();
    pngEncoder.CompressionLevel = PngCompressionLevel.BestCompression;
    await image.SaveAsync(imageStream, pngEncoder);
    imageStream.Seek(0, SeekOrigin.Begin);
}
private static Configuration GetImageSharpConfiguration()
{
    var configuration = new Configuration();
    configuration.ReadOrigin = ReadOrigin.Begin;
    return configuration;
}

You haven't populated the new Configuration you have generated with any codecs.

If you want to return a new configuration containing all the existing codecs you should do this.

```c#
private static Configuration GetImageSharpConfiguration()
{
var config = new Configuration(
new PngConfigurationModule(),
new JpegConfigurationModule(),
new GifConfigurationModule(),
new BmpConfigurationModule(),
new TgaConfigurationModule());

config.ReadOrigin = ReadOrigin.Current;
return config;

}

If you want to reset the stream everywhere you use the library you can set the default config instead.

```c#
var config = Configuration.Default;
config.ReadOrigin = ReadOrigin.Begin;

Or change Configuration.Default.ReadOrigin, or (IMO the best solution) just imageStream.Seek(0, SeekOrigin.Begin) before the image loads on misaligned streams.

Yup, I would choose to be explicit and reset my stream positions. Less possibility of unintended results.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marcpabst picture marcpabst  路  3Comments

xakep139 picture xakep139  路  3Comments

Sergio0694 picture Sergio0694  路  3Comments

antonfirsov picture antonfirsov  路  3Comments

vad3x picture vad3x  路  4Comments