Well, I spent half the day trying people's various solutions to the problem that Graphics.MeasureString() does
not measure the number of pixels wide a piece of text is like it claims to. People that posted their solutions
got it to work for them, but none of them are even close in my sample code. My sample WPF application is below,
but my question is just: What exactly is the problem with Graphics.MeasureString() that I need to work around?
The only difference I am aware of from the people that say they successfully worked around it is that they are
using Forms, and I am just using WPF--but I access Forms to get a Graphics object to work with.
Graphics.MeasureString() returns a bad width no matter what I try (different font, fixed font, AntiAlias
hinting). To determine what the correct answer should be (on my computer), I Alt-PrintScreened the displayed string to a
bitmap editor and counted the number of pixels wide it is: 130. All my attemped solutions return a width over
177. Following my various measuring functions is some code to try to take advantage of Win32's text measuring
capabilities, but I was not able to get the IDE to accept them.
The string is being displayed in a TextBox on a Canvas in the app's MainWindow. I am using Visual C# 2010 Express.
View Complete Post