Font.GetHeight Method

Definition

Returns the line spacing of this font.

Overloads

GetHeight()

Returns the line spacing, in pixels, of this font.

GetHeight(Graphics)

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

GetHeight(Single)

Returns the height, in pixels, of this Font when drawn to a device with the specified vertical resolution.

GetHeight()

Source:
Font.cs
Source:
Font.cs
Source:
Font.cs
Source:
Font.cs
Source:
Font.cs
Source:
Font.cs

Returns the line spacing, in pixels, of this font.

C#
public float GetHeight();

Returns

The line spacing, in pixels, of this font.

Remarks

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 screen display. 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. For a screen display that has a vertical resolution of 96 dots per inch, you can calculate the height as follows:

2355*(0.3/2048)*96 = 33.11719

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

GetHeight(Graphics)

Source:
Font.cs
Source:
Font.cs
Source:
Font.cs
Source:
Font.cs
Source:
Font.cs
Source:
Font.cs

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

C#
public float GetHeight(System.Drawing.Graphics graphics);

Parameters

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

Returns

The line spacing, in pixels, of this font.

Exceptions

graphics is null.

Examples

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.

C#
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));
}

Remarks

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

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

GetHeight(Single)

Source:
Font.cs
Source:
Font.cs
Source:
Font.cs
Source:
Font.cs
Source:
Font.cs
Source:
Font.cs

Returns the height, in pixels, of this Font when drawn to a device with the specified vertical resolution.

C#
public float GetHeight(float dpi);

Parameters

dpi
Single

The vertical resolution, in dots per inch, used to calculate the height of the font.

Returns

The height, in pixels, of this Font.

Remarks

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 screen display. 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 specified vertical resolution is 96 dots per inch, the height is calculated as follows:

2355*(0.3/2048)*96 = 33.1171875

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10