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

运行程序触发System.Drawing.SafeNativeMethods+Gdip.CheckStatus异常如何解决?

问题排查与解决方案

错误根因定位

从报错堆栈可以直接定位到错误触发点:

ArgumentException: 参数无效。
触发行:Bitmap source_Bitmap = new Bitmap(input_Image_Path);
这个错误是System.Drawing底层GDI+抛出的通用参数无效异常,常见触发原因和对应解决方法如下:

常见触发原因

  • 路径参数异常:
    • 传入的input_Image_Path为空、包含非法字符、对应的文件不存在,或者IFormFile还未完全写入到磁盘路径就调用了缩放方法读取文件
    • 路径为相对路径时,程序当前工作目录和预期不符,导致无法找到目标图片
  • System.Drawing兼容性限制:
    你之前的项目大概率是.NET Framework的Windows桌面项目,System.Drawing.Common库原生只支持Windows系统:
    • 如果当前项目是.NET Core/.NET 5+版本,默认已经将System.Drawing.Common标记为Windows-only API,非Windows环境下直接抛出该异常,Windows环境下也会有兼容性警告(就是你看到的绿色波浪线)
  • 代码存在隐性bug:
    你当前代码中获取编解码器用的是GetImageDecoders()(解码器),但保存图片需要的是编码器,虽然部分场景下能匹配到对应对象,但一旦返回null就会触发异常。

解决步骤

  1. 先加参数校验,排除路径问题:
    在实例化Bitmap之前先校验输入路径合法性:
if(string.IsNullOrWhiteSpace(input_Image_Path)) 
    throw new ArgumentNullException(nameof(input_Image_Path));
if(!File.Exists(input_Image_Path))
    throw new FileNotFoundException("输入图片不存在", input_Image_Path);

运行后如果触发上述自定义异常,直接排查上游imgPath的生成逻辑,确认IFormFile已经完整写入到对应路径后再调用缩放方法。
2. 修复System.Drawing兼容性问题:

  • 若项目只需要在Windows环境运行:
    打开项目的.csproj文件,在<PropertyGroup>节点下添加配置关闭兼容性警告:
    <EnableWindowsTargeting>true</EnableWindowsTargeting>
    
  • 若项目需要跨平台运行:
    直接替换System.Drawing.Common为跨平台图像库,推荐使用SixLabors.ImageSharp,无需依赖GDI+,接口更易用。
  1. 修复代码隐性bug,优化资源释放逻辑:
    所有实现了IDisposable的GDI对象全部用using块包裹,避免异常场景下资源泄漏,同时将解码器替换为编码器:
public static class ImageConvertor
{
    public static void Image_resize(string input_Image_Path, string output_Image_Path, int new_Width)
    {
        const long quality = 50L;
        // 参数校验
        if(string.IsNullOrWhiteSpace(input_Image_Path)) 
            throw new ArgumentNullException(nameof(input_Image_Path));
        if(string.IsNullOrWhiteSpace(output_Image_Path)) 
            throw new ArgumentNullException(nameof(output_Image_Path));
        if(new_Width <= 0)
            throw new ArgumentOutOfRangeException(nameof(new_Width), "宽度必须大于0");
        if(!File.Exists(input_Image_Path))
            throw new FileNotFoundException("输入图片不存在", input_Image_Path);

        using Bitmap source_Bitmap = new Bitmap(input_Image_Path);
        double dblWidth_origial = source_Bitmap.Width;
        double dblHeigth_origial = source_Bitmap.Height;
        double relation_heigth_width = dblHeigth_origial / dblWidth_origial;
        int new_Height = (int)(new_Width * relation_heigth_width);

        using var new_DrawArea = new Bitmap(new_Width, new_Height);
        using var graphic_of_DrawArea = Graphics.FromImage(new_DrawArea);
        // 设置绘制参数
        graphic_of_DrawArea.CompositingQuality = CompositingQuality.HighSpeed;
        graphic_of_DrawArea.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphic_of_DrawArea.CompositingMode = CompositingMode.SourceCopy;
        // 绘制缩放后图片
        graphic_of_DrawArea.DrawImage(source_Bitmap, 0, 0, new_Width, new_Height);

        // 保存输出
        using var output = System.IO.File.Open(output_Image_Path, FileMode.Create);
        var qualityParamId = System.Drawing.Imaging.Encoder.Quality;
        using var encoderParameters = new EncoderParameters(1);
        encoderParameters.Param[0] = new EncoderParameter(qualityParamId, quality);
        // 注意这里用GetImageEncoders获取编码器
        var codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == ImageFormat.Jpeg.Guid);
        if(codec == null)
            throw new InvalidOperationException("未找到JPEG编码器");
        new_DrawArea.Save(output, codec, encoderParameters);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 23:45:01