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;
}