Files
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

3.7 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Obtain Font Metrics 03/30/2017
csharp
vb
fonts [Windows Forms], obtaining metrics
font metrics [Windows Forms], obtaining
ff7c0616-67f7-4fa2-84ee-b8d642f2b09b

How to: Obtain Font Metrics

The xref:System.Drawing.FontFamily class provides the following methods that retrieve various metrics for a particular family/style combination:

The values returned by these methods are in font design units, so they are independent of the size and units of a particular xref:System.Drawing.Font object.

The following illustration shows the various metrics:

Illustration of font metrics: ascent, descent, and line spacing.

Example

The following example displays the metrics for the regular style of the Arial font family. The code also creates a xref:System.Drawing.Font object (based on the Arial family) with size 16 pixels and displays the metrics (in pixels) for that particular xref:System.Drawing.Font object.

The following illustration shows the output of the example code:

Example code output of Arial font metrics.

Note the first two lines of output in the preceding illustration. The xref:System.Drawing.Font object returns a size of 16, and the xref:System.Drawing.FontFamily object returns an em height of 2,048. These two numbers (16 and 2,048) are the key to converting between font design units and the units (in this case pixels) of the xref:System.Drawing.Font object.

For example, you can convert the ascent from design units to pixels as follows:

Formula showing the conversion from design units to pixels

The following code positions text vertically by setting the xref:System.Drawing.PointF.Y%2A data member of a xref:System.Drawing.PointF object. The y-coordinate is increased by font.Height for each new line of text. The xref:System.Drawing.Font.Height%2A property of a xref:System.Drawing.Font object returns the line spacing (in pixels) for that particular xref:System.Drawing.Font object. In this example, the number returned by xref:System.Drawing.Font.Height%2A is 19. Note that this is the same as the number (rounded up to an integer) obtained by converting the line-spacing metric to pixels.

Note that the em height (also called size or em size) is not the sum of the ascent and the descent. The sum of the ascent and the descent is called the cell height. The cell height minus the internal leading is equal to the em height. The cell height plus the external leading is equal to the line spacing.

[!code-csharpSystem.Drawing.FontsAndText#71] [!code-vbSystem.Drawing.FontsAndText#71]

Compiling the Code

The preceding example is designed for use with Windows Forms, and it requires xref:System.Windows.Forms.PaintEventArgs e, which is a parameter of xref:System.Windows.Forms.PaintEventHandler.

See also