3

I feel like I've read a question similar to this but I can't find it, so please close if duplicate.

I am trying to draw text with Graphics::DrawString with a monospace font (Consolas). However, when I draw the text, the letters are not evenly spaced. Here's what it looks like:

enter image description here

As you can see, the letters are clustered into groups of two and three. I read this article and I thought I had taken adequate steps to avoid it by using StringFormat::GenericTypographic, but apparently not. I am also using TextRenderingHintClearTypeGridFit because all the others look like junk (and there's no TextRenderingHintClearType without the GridFit part).

How can I draw text like all the other programs that draw text with a monospace font so that it looks right?

sehe
  • 374,641
  • 47
  • 450
  • 633
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • This is a known problem with Graphics.DrawString(). That's why the TextRenderer class became available in .NET 2.0, it doesn't have this problem. – Hans Passant Feb 17 '12 at 01:09
  • @HansPassant I'm not using .NET – Seth Carnegie Feb 17 '12 at 01:10
  • 2
    Well, use DrawTextEx() then, same thing. – Hans Passant Feb 17 '12 at 01:12
  • @HansPassant that's part of GDI, no? Does it do antialiasing, cleartype, and all that? – Seth Carnegie Feb 17 '12 at 01:14
  • 1
    Whatever is selected as the operating system default. Yes. – Hans Passant Feb 17 '12 at 01:23
  • @HansPassant if you would post that as an answer I'd be glad to accept it – Seth Carnegie Feb 17 '12 at 01:23
  • The issue is that GDI+ uses a different (and long since abandoned) system for drawing text. Starting with .NET framework 2.0, Microsoft changed all the controls to actually use **GDI** for text rendering, rather than **GDI+**. Text rendering in GDI is hardware accelerated, and continued to get improvements with character rendering, Uniscribe, ligatures. Text rendering in GDI+ is not hardware accelerated, and not getting any fixes or improvements. – Ian Boyd Feb 17 '12 at 02:44
  • @IanBoyd good to know. For some reason I always read that GDI+ is good for rendering text. I guess I can use GDI+ for some drawing for its antialiasing capabilities and plain GDI for text. – Seth Carnegie Feb 17 '12 at 02:46
  • GDI can also draw anti-aliased, use `CLEARTYPE_QUALITY` or `ANTIALIASED_QUALITY`. See my screenshots in http://stackoverflow.com/a/6404811/12597 for examples. – Ian Boyd Feb 17 '12 at 02:52
  • @IanBoyd wow that's an awesome answer. If you'd care to post an answer in this question too I'd upvote and mark it as the answer if Hans Passant doesn't answer in a while. – Seth Carnegie Feb 17 '12 at 02:55
  • Those definitely look evenly-spaced to me... Maybe it's my monitor? Or perhaps my eyes? Anyway, Hans and Ian are right, GDI is a better solution for drawing text regardless. – Cody Gray - on strike Feb 17 '12 at 03:19

1 Answers1

5

The issue is that GDI+ uses a different (and long since abandoned) system for drawing text. Starting with .NET framework 2.0, Microsoft changed all the controls to actually use GDI for text rendering, rather than GDI+. Text rendering in GDI is hardware accelerated, and continued to get improvements with character rendering, Uniscribe, ligatures. Text rendering in GDI+ is not hardware accelerated, and not getting any fixes or improvements.

GDI can also draw anti-aliased, use CLEARTYPE_QUALITY or ANTIALIASED_QUALITY.

Here's a comparison of

  • GDI+ (Graphics.DrawString)
  • GDI (TextRenderer.DrawText)

enter image description here

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219