2

I am trying to render some kind of a picture using GDI+ with dynamically loaded fonts. Here is a minimal example (.NET 8.0):

private void Form1_Load(object sender, EventArgs e)
{
    PrivateFontCollection collection = new();
    collection.AddFontFile("Unbounded-Medium.ttf");

    Bitmap bitmap = new(1000, 1000);
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
        Font font = new(collection.Families[0], 100, FontStyle.Bold, GraphicsUnit.Point);
        g.DrawString("Bbbb", font, Brushes.Red, new PointF(10, 10));
    }
    pictureBox1.Image = new Bitmap(bitmap, 1000, 1000);
}

Everything works fine on multiple Win 10 machines:

enter image description here

But on a Win 7 machines the same code produces the following output:

enter image description here

For some reason, some letters are being rendered... questionably. Other letters look normal though. Using trial-and-error approach I've found out that this kind of rendering happens:

  1. Only on Win 7 machines
  2. Only for font sizes above 70
  3. Only for certain fonts (Unbounded-Medium in my case)
  4. Only while using GDI+

I tried:

  1. Changing g.TextRenderingHint
  2. Adding StringFormat.GenericTypographic parameter to the DrawString method
  3. Enabling ClearType on the OS level

Why does this happen? Is this a Win 7 GDI+ bug? Is it possible to fix this issue without updating OS?

8
  • 4
    Unfortunately Windows 7 reached end of life in 2020. Commented Aug 2 at 18:58
  • 2
    This is a known limitation of GDI+ on Windows 7, especially when rendering private fonts (PrivateFontCollection) at large sizes. Commented Aug 2 at 19:03
  • @Itallmakescents I tried using .NET 6.0 and 8.0 Commented Aug 2 at 19:12
  • 1
    If you refer to the URLs in my previous post, you'll see that the release date for .NET 6 was after the end of support date for Windows 7. The following may also be of interest: What's new in .NET 5. Commented Aug 2 at 19:19
  • 1
    If you're looking to support out of support operating systems, the stick to .NET Framework <= 4.8 Commented Aug 2 at 19:36

1 Answer 1

3

Since Windows 7 reached the end of life in 2020 and it is a limitation of GDI+ on Windows 7, especially when rendering private fonts (PrivateFontCollection) at large sizes, you want to try maybe the alternative

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.