0

Trying to load a image with ImageSharp, I get this: Image cannot be loaded. Available decoders:

  • BMP : BmpDecoder
  • GIF : GifDecoder
  • PBM : PbmDecoder
  • JPEG : JpegDecoder
  • PNG : PngDecoder
  • TGA : TgaDecoder
  • Webp : WebpDecoder
  • TIFF : TiffDecoder

I try to load in 2 ways. The first is with this method, this received a byte array from other method where I load with a File System.

private byte[] FileToSizedImageSource1(String file, int width, int height)
    {
        byte[] data = File.ReadAllBytes(file);
        return this.FileToSizedImageSource2(data, width, height);
    }

private byte[] FileToSizedImageSource2(byte[] data, int x, int y)
    {
        byte[] result = null;
        using (MemoryStream memoryStream = new MemoryStream(data))
        {
            memoryStream.Seek(0, SeekOrigin.Begin);
            memoryStream.Position = 0;
            var raw = Image.Load(memoryStream);
            var clone = raw.Clone(context => context
                        .Resize(new ResizeOptions
                        {
                            Mode = ResizeMode.Max,
                            Size = new Size(x, y)
                        }));
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Position = 0;
                clone.Save(ms, new PngEncoder { CompressionLevel = PngCompressionLevel.DefaultCompression });
                byte[] img_buffer = ms.ToArray();
                result = Compress(img_buffer);
            }
        }
        return result;
    }

And the other way is loading with the own ImageSharp

private byte[] FileToSizedImageSource2(string path, int x, int y)
    {
        byte[] result = null;
        using (var raw = Image.Load(path))
        {
            var clone = raw.Clone(context => context
                        .Resize(new ResizeOptions
                        {
                            Mode = ResizeMode.Max,
                            Size = new Size(x, y)
                        }));
            using (MemoryStream ms = new MemoryStream())
            {
                ms.Position = 0;
                clone.Save(ms, new PngEncoder { CompressionLevel = PngCompressionLevel.DefaultCompression });
                byte[] img_buffer = ms.ToArray();
                result = Compress(img_buffer);
            }
        }
        return result;
    }
soyunewok
  • 115
  • 7
  • What kind of image is it? Where did it come from? – Retired Ninja May 11 '23 at 04:14
  • @RetiredNinja I edited the post, I tried to load a jpg and png image, in this example is png image. – soyunewok May 11 '23 at 04:35
  • Are you 100% sure, the file is ok? Can you display/open it in other applications/viewers? – Fildor May 11 '23 at 06:39
  • @Fildor absolutely – soyunewok May 11 '23 at 16:32
  • what line are you failing at exactly? your not trying to open the `result` byte array as an image are you and thats the part thats failing .... without the source image thats failing no one will really be able to debug this. and if your failing at on of the visible `Image.Load(...)` calls then the rest of the code is not helpfull to your issue and probably shouldn't be included... you want your test/error code to be as minimal as possible. – tocsoft May 11 '23 at 19:29
  • @tocsoft i think the line that is failing is the clone.Save(....), commenting that ```using``` block, i dont have error. – soyunewok May 11 '23 at 20:02
  • Can you please visit the discussions channel here bringing the image in question? https://github.com/SixLabors/ImageSharp/discussions/categories/q-a – James South Jul 12 '23 at 01:22

0 Answers0