2

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.

SiliconMind
  • 2,185
  • 4
  • 25
  • 49
  • It is unclear what you hope to achieve with a debugger, it isn't going to tell you what is wrong with the font or how the security update made the font incompatible. Only real solution you have is to stop using it until some magic day that a freeware font author is going to fix his font. That you've been distributing a font that *appears* to be exploiting a security hole ought to give you a some pause as well btw, be sure to let your customers know. – Hans Passant May 21 '15 at 18:51
  • What I want to achieve is to find out what is causing this issue and if there are any workarounds. The font I've linked is just an example. Since I posted this question I got reports on many other fonts, so the problem is not uncommon. If it was one or two fonts distributed with my software then it would be fairly easy to fix as it seams some fonts just have some weird issues. But that's not the case. I know that I can always add additional check before font is used and say to the user: hey this font worked before but now it's a security threat and we can't use it anymore. It's just stupid. – SiliconMind May 21 '15 at 19:11

1 Answers1

2

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

tunnelduck
  • 21
  • 1
  • There is an optional Windows Update now that fixes it, kb3065979: https://support.microsoft.com/en-us/kb/3065979. You can install it using Windows Update, it has to be selected explicitly as it is an optional update. – Wout Jul 02 '15 at 12:01