TextRenderer.DrawText renders Arial differently on XP vs Vista

Posted by Michael on Stack Overflow See other posts from Stack Overflow or by Michael
Published on 2010-04-08T06:25:26Z Indexed on 2010/04/09 12:13 UTC
Read the original article Hit count: 389

I have a c# application that does text rendering, something on par with a simple wysiwyg text editor.

I'm using TextRenderer.DrawText to render the text to the screen and GetTextExtentPoint32 to measure text so I can position different font styles/sizes on the same line.

In Vista this all works fine. In XP however, Arial renders differently, certain characters like 'o' and 'b' take up more width than in Vista. GetTextExtentPoint32 seems to be measuring the string as it would in Vista though, with the smaller widths. The end result is that every now and then a run of text will overlap the text preceding it because the preceding text gets measured as smaller than it actually is on the screen.

Also, my text rendering code mimics ie's text rendering exactly (for simple formatting and english language only) and ie text rendering seems to be consistent between vista and xp - that's how I noticed the change in size of the different characters.

Anyone have any ideas about what's going on?

In short, TextRenderer.DrawText and GetTextExtentPoint32 don't match up in xp for Arial. DrawText seems to draw certain characters larger and/or smaller than it does in Vista but GetTextExtentPoint32 seems to be measuring the text as it would in Vista (which seems to match the text rendering in ie on both xp and vista). Hope that makes sense.

Note: unfortunately TextRenderer.MeasureString isn't fast or accurate enough to meet my requirements. I tried using it and had to rip it out.

© Stack Overflow or respective owner

Related posts about c#

Related posts about text