Export (0) Print
Expand All

Font.GetHeight Method (Graphics)

Returns the line spacing, in the current unit of a specified Graphics, of this font.

Namespace:  System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

public float GetHeight(
	Graphics graphics
)

Parameters

graphics
Type: System.Drawing.Graphics

A Graphics that holds the vertical resolution, in dots per inch, of the display device as well as settings for page unit and page scale.

Return Value

Type: System.Single
The line spacing, in pixels, of this font.

ExceptionCondition
ArgumentNullException

graphics is null.

The line spacing of a Font is the vertical distance between the base lines of two consecutive lines of text. Thus, the line spacing includes the blank space between lines along with the height of the character itself.

If the Unit property of the font is set to anything other than GraphicsUnit.Pixel, the height, in pixels, is calculated using the vertical resolution of the specified Graphics object. For example, suppose the font unit is inches and the font size is 0.3. Also suppose that for the corresponding font family, the em-height is 2048 and the line spacing is 2355. If the Graphics object has a Unit property value of GraphicsUnit.Pixel and a DpiY property value of 96 dots per inch, the height is calculated as follows:

2355*(0.3/2048)*96 = 33.1171875

Continuing with the same example, suppose the Unit property of the Graphics object is set to GraphicsUnit.Millimeter rather than GraphicsUnit.Pixel. Then (using 1 inch = 25.4 millimeters) the height, in millimeters, is calculated as follows:

2355*(0.3/2048)25.4 = 8.762256

The following code example is designed for use with Windows Forms, and it requires PaintEventArgs e, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a Font.

  • Draws a line of text to the screen, using the new Font.

  • Gets the height of the font.

  • Draws a second line of text directly below the first line.

public void GetHeight_Example(PaintEventArgs e)
{

    // Create a Font object.
    Font myFont = new Font("Arial", 16);

    //Draw text to the screen with myFont.
    e.Graphics.DrawString("This is the first line",myFont,
        Brushes.Black, new PointF(0, 0));

    //Get the height of myFont. 
    float height = myFont.GetHeight(e.Graphics);

    //Draw text immediately below the first line of text.
    e.Graphics.DrawString(
        "This is the second line",
        myFont,
        Brushes.Black,
        new PointF(0, height));
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft