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


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.

Exception Condition

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 PaintEventArgse, 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.
        "This is the second line",
        new PointF(0, height));

.NET Framework
Available since 1.1
Return to top