共用方式為


HOW TO:取得字型度量資訊

FontFamily 類別提供下列方法,用來擷取特定家族/樣式組合的各種度量資訊 (Metric):

這些方法傳回來的數字是以字型設計單位為其單位,因此與特定 Font 物件的大小和單位無關。

下圖顯示的是不同的矩陣。

字型文字

範例

下列範例會顯示 Arial 字型家族標準樣式的矩陣。 程式碼也會建立大小為 16 個像素的 Font 物件 (根據 Arial 家族),並顯示該特定 Font 物件的度量資訊 (以像素為單位)。

下圖顯示的是範例程式碼的輸出。

字型文字

請注意上圖中輸出的最前面兩行。 Font 物件會傳回 16 個像素的大小,而 FontFamily 物件則是傳回 2,048 em 高度。 這兩個數字 (16 和 2,048) 是在字型設計單位和 Font 物件單位 (在這個範例中為像素) 之間轉換的關鍵。

例如,您可以依照下列方式,將上升部分從設計單位轉換為像素:

字型文字

下列程式碼藉由設定 PointF 物件的 Y 資料成員,來垂直放置文字。 每一個新文字行的 Y 座標都會增加 font.Height。 Font 物件的 Height 屬性會傳回該特定 Font 物件的行距 (以像素為單位)。 在這個範例中,Height 傳回的數字是 19。 請注意,這個數字與轉換行距度量資訊為像素時所取得的數字 (四捨五入為整數) 相同。

請注意,em 高度 (也稱為大小或 em 大小) 不是上升部分和下降部分的總和。 上升部分和下降部分的總和稱為儲存格高度。 儲存格高度減去內部前置字元就等於 em 高度。 儲存格高度加上外部前置字元等於行距。

        Dim infoString As String = "" ' enough space for one line of output
        Dim ascent As Integer ' font family ascent in design units
        Dim ascentPixel As Single ' ascent converted to pixels
        Dim descent As Integer ' font family descent in design units
        Dim descentPixel As Single ' descent converted to pixels
        Dim lineSpacing As Integer ' font family line spacing in design units
        Dim lineSpacingPixel As Single ' line spacing converted to pixels
        Dim fontFamily As New FontFamily("Arial")
        Dim font As New Font( _
           fontFamily, _
           16, _
           FontStyle.Regular, _
           GraphicsUnit.Pixel)
        Dim pointF As New PointF(10, 10)
        Dim solidBrush As New SolidBrush(Color.Black)

        ' Display the font size in pixels.
        infoString = "font.Size returns " & font.Size.ToString() & "."
        e.Graphics.DrawString(infoString, font, solidBrush, pointF)

        ' Move down one line.
        pointF.Y += font.Height

        ' Display the font family em height in design units.
        infoString = "fontFamily.GetEmHeight() returns " & _
           fontFamily.GetEmHeight(FontStyle.Regular) & "."
        e.Graphics.DrawString(infoString, font, solidBrush, pointF)

        ' Move down two lines.
        pointF.Y += 2 * font.Height

        ' Display the ascent in design units and pixels.
        ascent = fontFamily.GetCellAscent(FontStyle.Regular)

        ' 14.484375 = 16.0 * 1854 / 2048
        ascentPixel = _
           font.Size * ascent / fontFamily.GetEmHeight(FontStyle.Regular)
        infoString = "The ascent is " & ascent & " design units, " & ascentPixel _
           & " pixels."
        e.Graphics.DrawString(infoString, font, solidBrush, pointF)

        ' Move down one line.
        pointF.Y += font.Height

        ' Display the descent in design units and pixels.
        descent = fontFamily.GetCellDescent(FontStyle.Regular)

        ' 3.390625 = 16.0 * 434 / 2048
        descentPixel = _
           font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular)
        infoString = "The descent is " & descent & " design units, " & _
           descentPixel & " pixels."
        e.Graphics.DrawString(infoString, font, solidBrush, pointF)

        ' Move down one line.
        pointF.Y += font.Height

        ' Display the line spacing in design units and pixels.
        lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular)

        ' 18.398438 = 16.0 * 2355 / 2048
        lineSpacingPixel = _
           font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular)
        infoString = "The line spacing is " & lineSpacing & " design units, " & _
           lineSpacingPixel & " pixels."
        e.Graphics.DrawString(infoString, font, solidBrush, pointF)

string infoString = "";  // enough space for one line of output
int ascent;             // font family ascent in design units
float ascentPixel;      // ascent converted to pixels
int descent;            // font family descent in design units
float descentPixel;     // descent converted to pixels
int lineSpacing;        // font family line spacing in design units
float lineSpacingPixel; // line spacing converted to pixels

FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
   fontFamily,
   16, FontStyle.Regular,
   GraphicsUnit.Pixel);
PointF pointF = new PointF(10, 10);
SolidBrush solidBrush = new SolidBrush(Color.Black);

// Display the font size in pixels.
infoString = "font.Size returns " + font.Size + ".";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.
pointF.Y += font.Height;

// Display the font family em height in design units.
infoString = "fontFamily.GetEmHeight() returns " +
   fontFamily.GetEmHeight(FontStyle.Regular) + ".";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down two lines.
pointF.Y += 2 * font.Height;

// Display the ascent in design units and pixels.
ascent = fontFamily.GetCellAscent(FontStyle.Regular);

// 14.484375 = 16.0 * 1854 / 2048
ascentPixel =
   font.Size * ascent / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The ascent is " + ascent + " design units, " + ascentPixel +
   " pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.
pointF.Y += font.Height;

// Display the descent in design units and pixels.
descent = fontFamily.GetCellDescent(FontStyle.Regular);

// 3.390625 = 16.0 * 434 / 2048
descentPixel =
   font.Size * descent / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The descent is " + descent + " design units, " +
   descentPixel + " pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);

// Move down one line.
pointF.Y += font.Height;

// Display the line spacing in design units and pixels.
lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);

// 18.398438 = 16.0 * 2355 / 2048
lineSpacingPixel =
font.Size * lineSpacing / fontFamily.GetEmHeight(FontStyle.Regular);
infoString = "The line spacing is " + lineSpacing + " design units, " +
   lineSpacingPixel + " pixels.";
e.Graphics.DrawString(infoString, font, solidBrush, pointF);

編譯程式碼

前述範例是專為與 Windows Form 搭配使用而設計的,而且它需要有 PaintEventArgs e (這是 PaintEventHandler 的參數)。

請參閱

其他資源

Windows Form 中的圖形和繪圖

使用字型和文字