Calculating x-height in .NET 2.0
How开发者_开发知识库 can we calculate x-height of a font (typeface) without needing .NET 3.0 or later (for example I would like to avoid System.Windows.Media
namespace).
Although your question clearly states x-height, all the answers talk about height, which is not at all the same thing. MeasureString
and MeasureCharacterRanges
don't provide x-height.
Anyway, my answer would be: no, x-height is not available (in any way) in .Net 2.0.
Are you looking for this: Font.Height or Perhaps this: How to: Obtain Font Metrics
Or are you looking for a way to calculate the height of a text rendered with a font? In that case you can use the TextRenderer class with the MeasureText function. This is available from .NET 2.0.
If you may use the System.Drawing
namespace, you might want to use the Graphics.MeasureString()
method. Otherwise, do as Josh says, perhaps.
Your question might not be detailed enough for further assistance.
EDIT #1
The Graphics.MeasureString()
method's return type is a System.Drawing.SizeF
. This SizeF
class provides three properties which among them you have these two:
Height
;Width
.
If this is not what you want, them I don't get your point. If it is so, I'm sorry.
I hope this helps anyway! =)
All answers here are wrong. You CAN do that in .NET 2.0 but you need PInvoke.
You have to call the GetGlyphOutline()
API and pass one character: A lowercase "x".
From gmBlackBoxY
and gmptGlyphOrigin.y
you can calculate the x-height. This will require a TrueType or Vector font to be selected into the Device Context (DC).
WARNING: Be aware that not all fonts have lower case characters and that some fonts don't even define an "x" character. In this case the x-height cannot be determined.
精彩评论