Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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!?
Posted

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

 

I've 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.

  • *Experts*
Posted
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.
  • *Experts*
Posted
I don't know about for DD, but you might try one of the two alternatives (I would say try the second one, it's more accurate) on the page I linked to above.
  • *Experts*
Posted

I would recommend using the API functions to measure text and draw text. You won't get the anti-aliasing features of GDI+, but you also won't encounter the known bugs that exist in .NET GDI+.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...