When I say Graphics.MeasureString can hang with you, I mean it in a bad way!

by Michael S. Kaplan, published on 2010/07/22 07:01 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2010/07/22/10033828.aspx


I'm not up on the latest slang the kids today are using, you know?

I do have some of the most awesome cousins in the whole world and I am friends with them on Facebook and I can see what they say. Occasionally I can follow along, but usually I just accept that this is a dialect I cannot master....

It was just recently (in It's a bug, it's always been a bug. In either direction....) that I was talking about some specific issues that worked the way a user moight expect in Word and WordPad and RichEdit but did not behave in such a way when GDI+ was used.

The behavior is one requested/demanded in the current version of the Unicode Bidi algoroithm, so that really was not a bug!

This is not an issue like that.

No sir.

This is a bug

Everyone would call it such.

The description:

Hi all, 

I’m working on a bug complaining that Graphics.MeasureString hangs with a long right-to left string. If the number of ش  in a string is greater than 2046, it will hang. My question is: Is it a known issue that there is a length limitation in GdipMeasureString? Is there a workaround approach available for us? Your suggestions will be sincerely appreciated.

We can repro it with the following code: 

    class Program {
        static void Main(string[] args){
            Bitmap b = new Bitmap(10, 10);
            Graphics g = Graphics.FromImage(b);
            Font objFont = new Font("Arial", 10);
            //Notice: i == 2046 works well.
            //Notice: i == 2047 hangs.
            for (int i = 2000; i < 2500; i++) {
                // Create a string with a repetition of right-to-left characters.
                // If works well if use a normal char like 'a'.
                // string ss = new string('a', i);  //this one works fine
                // but it hangs when the length of ش is greater than 2047
                string ss = new string('ش', i);
                Console.WriteLine("Testing " + i.ToString());
                SizeF oSize = g.MeasureString(ss, objFont);
                Console.WriteLine("Size = " + oSize.Width);
                Console.WriteLine();
                Console.WriteLine();
            }
       }
    }

That sounds like a bug, right?

You can see the original report on the Connect site, right here.

You can see there the resolution, too:

Windows team has confirmed that it's a regression, but they decide not to fix in Win7 since a string with more than 2046 characters is a corner case. However, they would evaluate it for Win8.

Addtionally, we have a workaround, using Graphics.MeasureCharacterRanges. Thefore, I'm going to close this issue.

Thanks and keep the feedback coming.
UIFx Team

Kind of says it all, I guess. Right?

I'll be honest, they are making a pretty optimistic assessment as far as being willing to look at it in the future given how little GDI+ work is happening, but perhaps the hang elevates the status a bit.

Besides, who cares what Graphics.MeasureString is doing wrong as long as Graphics.MeasureCharacterRanges is around to pick up the slack.

In any case, when I explain how Graphics.MeasureString may hang with you, don't assume it's a good thing!


Maurice Bonne on 26 Jul 2010 10:39 PM:

Hello Michael, What i do not understand: in my original reprot i state clearly that also the MeasureChacterRanges does NOT work (atleast on my system). But still this is mentioned as a workaround. Did anybody actual test it?

PS: I do not knwon much about arabic languanges (only that we have customers there). the bug is realy a corner case because as soon as there are other charaters involved (point, comma, (i think left to right chars)) the methods work correrct. And i think that it is very unlikely that there is a onc sentence of 2047 characrters.

Michael S. Kaplan on 26 Jul 2010 10:49 PM:

For other people, it apparently did work (or maybe they misunderstood you). Though even if it did not, the support opf GDI+ issue is a real one, and the best answer at this point is to NOT ever use GDI+ at all....

Ian Boyd on 7 Nov 2010 4:01 AM:

Are there any alternatives to GDI+ for text rendering? The only ones i can think of are GDI and DirectWrite.

Michael S. Kaplan on 7 Nov 2010 4:49 AM:

Indeed -- GDI/Uniscribe (and stuff that uses it like TextRenderer), and DirectWrite are the big options....


Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

referenced by

2010/11/06 Please stop using this turd. And if you are an MS employee: stop using this turd. Now.

go to newer or older post, or back to index or month or day