.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Post New Web Links

WPF MeasureString (again)

Posted By:      Posted Date: October 16, 2010    Points: 0   Category :WPF

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

More Related Resource Links

Graphics.MeasureString inconsistency


I have two strings, in Times New Roman that are being measured by Graphics.MeasureString(). Viewing the strings in Word shows one string longer than the other, but MeasureString returns a smaller number for the longer string.

Here is the code:

    class Program
        public static System.Windows.Forms.Form form = new System.Windows.Forms.Form();
        public static Graphics GRAPHIX = form.CreateGraphics();

        static void Main(string[] args)
            string text;
            float total;

            Font TIMES = new Font("Times New Roman", 8);
            Font TIMES_BOLD = new Font("Times New Roman", 8, FontStyle.Bold);
            Font TIMES_SMALL = new Font("Times New Roman", 6);
            Font TIMES_ITALIC = new Font("Times New Roman", 8, FontStyle.Italic);

            text = "Bedbugs bite(s) - see Injury, superficial, by site";
            total = 0;
            total += GRAPHIX.MeasureString("Bedbugs bite(s)", TIMES_BOLD).Width;
            total += GRAPHIX.MeasureString(" - ", TIMES).Width;
            total += GRAPHIX.MeasureString("see", TIMES_ITALIC).Width;
            total += GRAPHIX.MeasureString(" ", TIMES).Width;
            total += GRAPHIX.MeasureString("Injury, superf
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend