Generic GDI+ exception in GraphicsPath.AddString() with certain fonts and characters

 ̄綄美尐妖づ 提交于 2019-12-10 11:03:16

问题


Any ideas why one of recent Windows updates causes issues with GraphicsPath.AddString() and some fonts but only for specific characters? How to fix that?

Example font is Thin Pencil Handwriting (it's public domain, so you can try for yourself). It works fine if you use Graphics.DrawString() with it. It also works fine if you try to use GraphicsPath.AddString() with it to write Lorem ipsum, but will fail if you try to use C character (capital c) or 5 digit.

It worked perfectly fine just few weeks ago and after recent updates it fails miserably with the infamous and virtually undebuggable Generic GDI+ exception.

Example code:

string textOK = "Lorem ipsum";
string broken = "C"; // Yes, only capital letter 'c'!

FontStyle style = FontStyle.Regular;

Bitmap b = new Bitmap(200, 200, PixelFormat.Format32bppPArgb)
using (Graphics g = Graphics.FromImage(b))
{
  g.Clear(Color.White);
  g.SmoothingMode = SmoothingMode.HighQuality;

  using (StringFormat sf = new StringFormat())
  {
    RectangleF rect = new RectangleF(0, 0, 200, 200);
    float size = 8;

    using (Font f = new System.Drawing.Font("Thin Pencil Handwriting", size, style))
    {
      // Works perfectly fine
      g.DrawString(textOK, f, Brushes.Black, rect, sf);
      g.DrawString(broken, f, Brushes.Black, rect, sf);
    }

    using (GraphicsPath path = new GraphicsPath())
    {
      FontFamily family = new FontFamily("Thin Pencil Handwriting");

      // This works fine
      path.AddString(textOK, family, (int)style, size, rect, sf);

      // This causes Generic GDI+ exception!
      path.AddString(broken, family, (int)style, size, rect, sf);

      g.FillPath(Brushes.Black, path);
    }
  }
}

I'm using C# but I don't thing it's particularly C# related. Older machines without recent updates works fine, but I can't tell people not to install system updates :( I've also spotted two other fonts that cause similar issues - both with GraphicsPath.AddString() method.


回答1:


I ran into the same issue after installing updates this morning. I found this recent post on Microsoft that seems to address it though the issue seems to still be outstanding.

https://connect.microsoft.com/VisualStudio/feedback/details/1331855/kb3045171-crash-gdi-with-system-drawing-drawing2d-addstring



来源:https://stackoverflow.com/questions/30346758/generic-gdi-exception-in-graphicspath-addstring-with-certain-fonts-and-charac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!