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

Image.Save报错:Value cannot be null. Parameter name: encoder 及图片格式保存问题

问题解决:Image.Save 报 "Value cannot be null. Parameter name: encoder" 错误并保留原格式

出现该错误的核心原因是:依赖img.RawFormat获取图片格式时,部分场景下无法正确映射到系统已安装的图像编码器,导致Save方法找不到对应编码器抛出异常。结合你仅处理.jpg/.jpeg/.png/.tiff格式的需求,可通过以下两种可靠方案解决:

方案1:通过文件名后缀明确指定格式

利用已知的文件名后缀直接确定目标格式,完全规避RawFormat的不确定性,这是最稳妥的方式:

修改代码中保存逻辑部分:

if (needsRotation)
{
    ImageFormat targetFormat;
    var fileExtension = Path.GetExtension(imgFileName).ToLowerInvariant();
    
    switch (fileExtension)
    {
        case ".jpg":
        case ".jpeg":
            targetFormat = ImageFormat.Jpeg;
            break;
        case ".png":
            targetFormat = ImageFormat.Png;
            break;
        case ".tiff":
            targetFormat = ImageFormat.Tiff;
            break;
        default:
            throw new NotSupportedException($"不支持处理该格式的图片:{fileExtension}");
    }

    // 针对JPEG格式,可额外指定保存质量(避免默认压缩导致画质下降)
    if (targetFormat == ImageFormat.Jpeg)
    {
        var jpegEncoder = ImageCodecInfo.GetImageEncoders()
            .FirstOrDefault(e => e.FormatID == ImageFormat.Jpeg.Guid);
        
        if (jpegEncoder != null)
        {
            var encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 90L); // 可根据需求调整质量值(0-100)
            img.Save(correctedStream, jpegEncoder, encoderParams);
        }
        else
        {
            img.Save(correctedStream, targetFormat);
        }
    }
    else
    {
        img.Save(correctedStream, targetFormat);
    }
    
    correctedStream.Position = 0;
    ImgDB.UploadImage(imgFileName, correctedStream);
}

方案2:主动获取对应格式的编码器

若要保留RawFormat的使用,可主动查找对应格式的编码器,找不到时再通过文件名兜底:

if (needsRotation)
{
    ImageFormat rawFormat = img.RawFormat;
    var targetEncoder = ImageCodecInfo.GetImageEncoders()
        .FirstOrDefault(e => e.FormatID == rawFormat.Guid);
    
    if (targetEncoder != null)
    {
        // JPEG格式单独处理质量参数
        if (rawFormat == ImageFormat.Jpeg)
        {
            var encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
            img.Save(correctedStream, targetEncoder, encoderParams);
        }
        else
        {
            img.Save(correctedStream, targetEncoder, null);
        }
    }
    else
    {
        // 编码器不存在时,通过文件名后缀确定格式
        var fileExtension = Path.GetExtension(imgFileName).ToLowerInvariant();
        switch (fileExtension)
        {
            case ".jpg":
            case ".jpeg":
                img.Save(correctedStream, ImageFormat.Jpeg);
                break;
            case ".png":
                img.Save(correctedStream, ImageFormat.Png);
                break;
            case ".tiff":
                img.Save(correctedStream, ImageFormat.Tiff);
                break;
            default:
                throw new NotSupportedException($"不支持的图片格式:{fileExtension}");
        }
    }
    
    correctedStream.Position = 0;
    ImgDB.UploadImage(imgFileName, correctedStream);
}

关键注意点

  • 优先通过文件名判断格式:因为你明确知道处理的图片格式范围,这种方式比依赖RawFormat更可靠。
  • JPEG格式建议指定质量:默认保存JPEG会自动压缩,通过EncoderParameter可以控制输出画质,避免不必要的质量损失。
  • 及时释放资源:你的代码中已经使用using和Dispose,保持这个习惯避免内存泄漏。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 00:42:57