天天看点

【排除解决】System.Runtime.InteropServices.ExternalException (0x80004005): GDI+ 中发生一般性错误

前言:

  今天项目发布上线,发布到正式环境验证功能的时候忽然方向之前做的一个图片合成的功能报错了提示:System.Runtime.InteropServices.ExternalException (0x80004005): GDI+ 中发生一般性错误。也就是说应用的System.Drawing中的Bitmap的这个类中的属性出了问题,这到底是什么问题呢?首先我本地开发,测试环境都可以正常的,为什么已发布到正式环境就有问题了呢,到底是环境问题还是配置权限的问题呢?

代码如下:

/// <summary>
        /// 合成图片
        /// </summary>
        /// <param name="backgroundImage">背景图</param>
        /// <param name="qrCodeImg">二维码图片</param>
        /// <param name="savePhysicalPath">图片存放物理路径</param>
        /// <param name="xDeviation">绘制图像X轴偏差</param>
        /// <param name="yDeviation">绘制图像Y轴偏差</param>
        /// <param name="width">绘制图像宽</param>
        /// <param name="height">绘制图像高</param>
        /// <returns></returns>
        public string CompositePicture(Image backgroundImage, Image qrCodeImg, string savePhysicalPath, int xDeviation = 0, int yDeviation = 0, int width = 0, int height = 0)
        {
            Bitmap bitmap = new Bitmap(backgroundImage.Width, backgroundImage.Height);
            Graphics graphics = Graphics.FromImage(bitmap);//绘图
            graphics.Clear(Color.White);
            SolidBrush surush = new SolidBrush(Color.White);
            graphics.DrawImage(backgroundImage, 0, 0, backgroundImage.Width, backgroundImage.Height);
            graphics.DrawImage(qrCodeImg, xDeviation, yDeviation, width, height);
            GC.Collect();//垃圾清理

            string compositePictureUrl = savePhysicalPath + Guid.NewGuid().ToString() + ".jpg";
            //合成图片保存
            bitmap.Save(compositePictureUrl, System.Drawing.Imaging.ImageFormat.Jpeg);

            return compositePictureUrl;
        }      

解决方案:

1、首先我排除了一下线上服务器是否存在savePhysicalPath图片存放的物理路径(我发布合成的图片保存的物理物理为/Content/Image)查看确实存在,因为之前在程序中写过判断文件夹是否存在不存在创建文件夹的逻辑。

if (!Directory.Exists(savePhysicalPath))
            {
                Directory.CreateDirectory(savePhysicalPath);
            }
      

2、判断文件Image是否存在读写的权限(让服务器管理员查了下果然是没有权限的),把权限的读写权限开通后就一切正常了。

【排除解决】System.Runtime.InteropServices.ExternalException (0x80004005): GDI+ 中发生一般性错误

继续阅读