Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a Font's Metrics in Pixels

Tags:

c#

.net

winforms

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?

like image 856
Jonathan Wood Avatar asked Nov 18 '12 18:11

Jonathan Wood


People also ask

How do you measure font size 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.

How do I measure text size?

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.

What are font metrics?

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.


1 Answers

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).

like image 98
Neolisk Avatar answered Oct 27 '22 03:10

Neolisk