前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【排查解决】System.Runtime.InteropServices.ExternalException (0x80004005): GDI+ 中发生一般性错误

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

作者头像
追逐时光者
发布2021-06-10 15:59:07
1.7K0
发布2021-06-10 15:59:07
举报
文章被收录于专栏:编程进阶实战编程进阶实战

前言:

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

代码如下:

代码语言:javascript
复制
        /// <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)查看确实存在,因为之前在程序中写过判断文件夹是否存在不存在创建文件夹的逻辑。

代码语言:javascript
复制
            if (!Directory.Exists(savePhysicalPath))
            {
                Directory.CreateDirectory(savePhysicalPath);
            }

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-06-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言:
  • 代码如下:
  • 解决方案:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档