Does anyone know *any* other way to determine string or character width?
Preferably, I would like to measure the width of a string, in a particular
font or font family, in a *device independent* manner, without using a
Graphics object. It is possible to measure the height in a
device-independent manner with FontFamily.GetEmHeight/GetCellAscent/etc; is
there a way to do this for the width?
Also, does anyone know whether MeasureString() takes into account Kerning
information?
MeasureString does not take kerning into account.
MeasureCharacterRanges does.
--
Bob Powell [MVP]
C#, System.Drawing
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
Read my Blog at http://bobpowelldotnet.blogspot.com
"David P" <qwer...@hotmail.com> wrote in message
news:%szQb.10653$5D.9760@clgrps13...
string measureString = "hello";
CharacterRange[] characterRanges =
{
new CharacterRange(0, 5),
};
RectangleF layoutRect = new RectangleF(0.0F, 0.0F, 100000.0F, 100000.0F);
StringFormat stringFormat = new StringFormat();
stringFormat.SetMeasurableCharacterRanges(characterRanges);
Region[] stringRegions = new Region[1];
stringRegions = e.Graphics.MeasureCharacterRanges(
measureString, font, layoutRect, stringFormat);
RectangleF measureRect = stringRegions[0].GetBounds(e.Graphics);
int Offset = measureRect.X;
int Width = measureRect.Width;
Isn't there a simpler way? I think if I use this method I'll want to build
a cache of word widths so as to avoid doing this thousands of times while
formatting a document...
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:ue3xNYr4...@TK2MSFTNGP09.phx.gbl...
I made a GIF that illustrates this, but unfortunately I apparently can't
send it to the newsgroup (I know it's a text newsgroup but the image is only
2KB) Anyway, the image shows the output of
Font f;
Brush b;
string s1 = "LQp";
SizeF s1size = e.Graphics.MeasureString(s1, f);
e.Graphics.DrawRectangle(new Pen(Color.Red), 0, 0, s1size.Width,
s1size.Height);
e.Graphics.DrawString(s1, f, b, 0, 0);
There is some unused space on the left and right sides of the string. Where
does this extra space come from? Can I get rid of it? Is it possible to
measure how much space is being added to the sides?
Additionally, the height returned by MeasureString is larger than the height
of the font (f.GetHeight). Why? (I do not believe the extra height is for
inter-line spacing; GetHeight() already includes enough space for
comfortable line spacing.)
By following advice near the bottom of
http://support.microsoft.com/?id=307208 I was able to get rid of the excess
space. Basically the solution was to make a "StringFormat" like this:
StringFormat sFormat = new StringFormat(StringFormat.GenericTypographic);
then pass it to MeasureString and DrawString. But bizarrely, when calling
MeasureString with a StringFormat, an extra argument is required, either a
width or an origin or both:
size = e.Graphics.MeasureString(s, f);
becomes
size = e.Graphics.MeasureString(s, f, new PointF(0,0), stringFormat);
What is the purpose of the origin argument? There should be no need for it
since I'm not actually drawing the string. As usual the documentation is
totally unhelpful.
Ron Allen
"David P" <qwer...@hotmail.com> wrote in message
news:SXDQb.11846$P51.1437@clgrps12...