Search code examples
imagesharp

Custom decoders for PNG files


I've donwloaded an image from user manual (see attachment) and need to transform it. When I tried to load it via following code, I got the exception: "Image cannot be loaded. Available decoders:\r\n - JPEG : JpegDecoder\r\n - PNG : PngDecoder\r\n - GIF : GifDecoder\r\n - BMP : BmpDecoder\r\n". Is it possible to apply custom decoder and where can I found them?

using (var originalImage = new MemoryStream(...))
using (var image = Image.Load<Rgba32>(originalImage))
{

}

Solution

  • The linked image can be decoded using ImageSharp. As @tocsoft says in the comments it likely you forgot to reset your input stream position.

    Here's the two images. The second we've loaded and flipped vertically using the following code:

    using (var image = Image.Load(Path.Combine(inPath, "-7bH2hfA.png")))
    {
        image.Mutate(x => x.Flip(FlipMode.Vertical));
    
        image.Save(Path.Combine(outPath, "-7bH2hfA-flipped.png"));
    }
    

    Your input image:

    Input image

    Our flipped output image:

    Flipped output image

    EDIT

    When I originally tested the image I used r-click save which gave me a valid png. I have since used the direct download from Dropbox which yields the original file.

    It's not a png! It is, in fact, a webp file.

    Hex Dump of original image