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

Azure托管Web应用升级.NET Framework后出现GDI+通用错误

生产环境偶发GDI+通用错误排查问题
  • 已查阅几乎所有同错误信息的相关问题,未找到解决方案。多数问题针对IIS托管应用,Azure托管相关的旧问题要么无解决方案,要么关联文章链接已失效。
  • 错误为偶发情况,仅在生产环境出现,QA、开发及本地环境均无法复现。错误在将.NET Framework从4.7升级至4.8的版本发布后立即出现(该版本无其他代码变更),显然QA与生产环境存在配置差异,但目前尚未定位到具体差异点。
  • 部分同类帖子指出该错误是权限问题,已核查所有可想到的权限设置,确认QA与生产环境权限一致。目前Azure运维人员正在全力对比两者的所有环境配置差异。

业务场景为生成条形码,错误发生在以下代码行:

graphic.DrawString(string.Format("*{0}*", Code), newFont, black, point);

完整方法代码:
(注:已知晓该代码可通过新版C#优化,目前正在推进相关工作!)

public static byte[] BarcodeImageGenerator(string Code)
{
    byte[] BarCode;
    int Height; // = 50
    int Width;// = 100

    Code = Code.ToUpper();
    PrivateFontCollection customfont = new PrivateFontCollection();
    customfont.AddFontFile(string.Format("{0}{1}", HttpRuntime.AppDomainAppPath, "fonts\\FRE3OF9X.TTF"));
    Font newFont = new Font(customfont.Families[0], 60, FontStyle.Regular);
    SizeF stringSize = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(Code, newFont);

    Width = (int)stringSize.Width + 50;
    Height = (int)stringSize.Height;
    Bitmap objBitmap = new Bitmap(Width, Height);
    using (Graphics graphic = Graphics.FromImage(objBitmap))
    {
        PointF point = new PointF(2f, 2f);
        SolidBrush black = new SolidBrush(Color.Black);
        SolidBrush white = new SolidBrush(Color.White);
        graphic.FillRectangle(white, 0, 0, objBitmap.Width, objBitmap.Height);
        graphic.DrawString(string.Format("*{0}*", Code), newFont, black, point);
    }          

    using (MemoryStream Mmst = new MemoryStream())
    {
        objBitmap.Save(Mmst, ImageFormat.Jpeg);
        BarCode = Mmst.GetBuffer();               
        return BarCode;
    }
}

堆栈跟踪前几行:

System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, PointF point)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 19:53:11