Regarding Graphics.MeasureString

OsirisGothra

Member
Joined
May 29, 2003
Messages
10
Location
Denver
For some reason, I can never get an accurate measurement of a string... is this a problem for anyone else or is there something I am missing? Usually the string measurement is fine on smaller strings but once it gets more than a few characters long I find that it is reporting that it is much longer than it really is.. (esp. with say exclamation points and periods and such involved) Also, I am setting the string format to measure trailing spaces when needed... I also realize the .Width and .Height properties are single precision and I round them off or just use them as is, but it just seems that the measurements are WAY OFF sometimes!! :mad: Can anyone help me here!?
 
this is in VB.NET

Dim W As Single
Dim cFont as New Font("Times New Roman",12,FontStyle.Regular)

v.FormatFlags = v.FormatFlags Or StringFormatFlags.MeasureTrailingSpaces
W = owner.CreateGraphics.MeasureString(X(I), cFont, owner.Width, v).Width

Ive tried
dest.X += Math.Round(W)
and
dest.X += W

and
dest.X += Convert.ToInt32(W)

The X coordinate seems to end up anywhere from 4 to 20 pixels away from the actual end of the string on screen.
 
Are you actually using the same Font object to draw with as you are to measure with?
 
Yes it is the same font object
Might i add that the string in question is being drawn by
a directdraw surface ddsurface.DrawText
 
If youre not drawing the text using GDI+, Im not at all surprised you get different results.
 
In theory, when you draw text at a particular size, it should be the same regardless of what you use to draw it. MeasureString does seem to be a bit inaccurate; perhaps it has something to do with variable-width fonts.
 
GDI+ uses grid fitting to draw text at sub-pixel accuracy, I wouldnt want to compare results from GDI+ and non GDI+ functions.
 
I dont know about for DD, but you might try one of the two alternatives (I would say try the second one, its more accurate) on the page I linked to above.
 
I would recommend using the API functions to measure text and draw text. You wont get the anti-aliasing features of GDI+, but you also wont encounter the known bugs that exist in .NET GDI+.

-Nerseus
 
Ok... yeah i fixed it using the GetTextExtentExPoint API function, works like a charm... :) YAY....

- Thanks Everyone For The Help!

If anyone ever has trouble with this type of thing ask me for details on getting that to work in .NET osirisgothra@hotmail.com
now im off to go make a new problem :)
 
Back
Top