Groups | Blog | Home
all groups > dotnet drawing api > december 2005 >

dotnet drawing api : Get the exact heigh of a String


bhavin
12/13/2005 11:27:12 AM
FontFamily.GetCellAscent() method should do the job.

Ofcourse it gives you the measurement in DesignUnits, which you can
easily convert to pixels using your font size and the
FontFamily.GetEmHeight() value.

hth
-bhavin

[quoted text, click to view]
Hans Peter Gerdes
12/13/2005 3:48:08 PM
I´m trying to get the exact heigh of a string ("X").
I try Graphics.Measurestring() or Font.GetHeight() but all function give me
the Height with ascent and descent of the font. I need exactly the Height of
the Character "X".

Thanks in advance

Hans Peter

Bob Powell [MVP]
12/13/2005 5:08:50 PM
AFAIK without examining the font tables directly there is no way to get this
information from the font.

You can employ a little bit of scan-line analysis to find the black pixels
on a bitmap.

See my article on extracting a region from a bitmap for the formula.

You don't actually need the region but the extents of the pixels can be
easily calculated.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

Hans Peter Gerdes
12/14/2005 6:06:43 AM
Thanks for answer, bhavin.

FontFamily.GetCellAscent() give me the "ascent" size of the Font. But the
size has an unknown "extra ascent".
The "extras ascent" isn't visible. I need only the black ascent of the Font.

Hans Peter

Hans Peter Gerdes
12/14/2005 6:32:53 AM
maybe there is a other way to do a DrawString centered in a rect

Hans Peter

Hans Peter Gerdes
12/14/2005 7:42:09 AM
i found the StringFormat.GenericTypographics and it helps to center the
string in a rect.

Chris Y
12/14/2005 1:49:57 PM

[quoted text, click to view]
For horizontal:center alignment, set StringFormat to StringAlignment.Center.

I too am trying to find a way to do vertical:middle. Is there an easy
method, that takes into account line wrapping as well?

Thanks.

Bob Powell [MVP]
12/17/2005 2:02:47 PM
The cell ascent of a character does not accurately include the actual height
of the glyphs within the font. To get this information exactly, as the OP
requires, one would have to examine the font tables. Interestingly, it's
also not against the rules for a glyph to extand past the declared
boundaries of the EM square either.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

AddThis Social Bookmark Button