I have a WinForms user control and I'm trying to obtain the exact horizontal character spacing for the current font, which is expected to be mono-spaced.
Looks like the font's Size
property provides this information but is apparently in points and I'm working in pixels.
var fontWidth = this.Font.Size; // Returns em-size in points--not pixels
If I create the font myself, I can specify that it uses Pixel units. But in my case the font is set through a property of my user control and I can't determine how the font is created. Unfortunately, the font's Unit
property is read-only.
How can I make an existing font return metrics in pixels?
1 pixel (px) is usually assumed to be 1/96th of an inch. 1 point (pt) is assumed to be 1/72nd of an inch. Therefore 16px = 12pt.
A font is often measured in pt (points). Points dictate the height of the lettering. There are approximately 72 (72.272) points in one inch or 2.54 cm. For example, the font size 72 would be about one inch tall, and 36 would be about a half of an inch.
Font metrics are measurements of text rendered by a Font object such as the height of a line of text in the font. The most common way to measure text is to use a FontMetrics instance which encapsulates this metrics information.
Please see this article on MSDN:
How to: Obtain Font Metrics
To get pixels, you use conversion formula.
descentPixel = font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular);
Also see Get single glyph metrics (.net).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With