You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ImageSharp加载图片失败,报错无对应解码器求解决方案

ImageSharp加载图片报错排查与解决

问题详情

加载图片时触发错误,提示:

Image cannot be loaded. Available decoders:

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

尝试过两种加载方式:

方式1:读取字节数组后处理

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

方式2:直接加载文件路径

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

排查与解决办法

1. 验证图片格式兼容性

报错列出的是当前ImageSharp支持的所有格式,若你加载的图片格式不在列表中(如HEIC、SVG、ICO等),会直接触发加载失败。

  • 解决:将图片转换为支持的格式,或添加对应格式的扩展解码器包(如针对HEIC的第三方扩展)。

2. 检查图片文件完整性

  • 先用系统自带图片查看器打开目标图片,确认文件未损坏。
  • 若使用方式1,检查File.ReadAllBytes读取的字节数组长度是否与文件实际大小一致,确保字节流完整。

3. 确认ImageSharp版本与解码器注册

  • 旧版本ImageSharp可能需要手动注册解码器,建议更新到最新稳定版本,新版本默认会自动注册所有内置解码器。
  • 若需手动注册解码器,可添加以下代码:
// 注册内置解码器示例
Configuration.Default.ImageFormatsManager.SetDecoder(BmpFormat.Instance, new BmpDecoder());
Configuration.Default.ImageFormatsManager.SetDecoder(JpegFormat.Instance, new JpegDecoder());
// 按需添加其他格式解码器

4. 修正字节流读取逻辑(方式1)

new MemoryStream(data)构造时默认Position为0,无需额外调用Seek和Position赋值,可简化代码避免潜在的流位置问题。若问题仍存在,尝试重新初始化MemoryStream确保读取起始位置正确。

5. 检查文件读取权限(方式2)

确认程序运行账户对目标文件路径拥有读取权限,权限不足会导致无法加载文件,触发加载失败错误。

内容的提问来源于stack exchange,提问作者soyunewok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 15:05:14