OsirisGothra Posted June 23, 2003 Posted June 23, 2003 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!? Quote
a_jam_sandwich Posted June 23, 2003 Posted June 23, 2003 What code are you using cause ive never had problems :) Andy Quote Code today gone tomorrow!
OsirisGothra Posted June 23, 2003 Author Posted June 23, 2003 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. Quote
*Gurus* divil Posted June 23, 2003 *Gurus* Posted June 23, 2003 Are you actually using the same Font object to draw with as you are to measure with? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
OsirisGothra Posted June 23, 2003 Author Posted June 23, 2003 Yes it is the same font object Might i add that the string in question is being drawn by a directdraw surface ddsurface.DrawText Quote
*Experts* Volte Posted June 23, 2003 *Experts* Posted June 23, 2003 Try looking at this page: http://www.codeproject.com/cs/media/measurestring.asp Quote
*Gurus* divil Posted June 23, 2003 *Gurus* Posted June 23, 2003 If you're not drawing the text using GDI+, I'm not at all surprised you get different results. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Volte Posted June 23, 2003 *Experts* Posted June 23, 2003 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. Quote
*Gurus* divil Posted June 23, 2003 *Gurus* Posted June 23, 2003 GDI+ uses grid fitting to draw text at sub-pixel accuracy, I wouldn't want to compare results from GDI+ and non GDI+ functions. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
OsirisGothra Posted June 24, 2003 Author Posted June 24, 2003 well then, is there a function simmilar to this for DirectDraw? Quote
*Experts* Volte Posted June 24, 2003 *Experts* Posted June 24, 2003 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. Quote
*Experts* Nerseus Posted June 27, 2003 *Experts* Posted June 27, 2003 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 Quote "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
OsirisGothra Posted June 28, 2003 Author Posted June 28, 2003 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 i'm off to go make a new problem :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.