如何获取字体尺寸

如何获取字体尺寸

Refs: https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-obtain-font-metrics  

字体族 FontFamily 类提供以下方法检索特定的fontFamily/FontStyle组合的各种指标

这些返回的数值是字体设计的单位,所以他们是独立于一个特定的字体对象。

下图显示的各种度量值。

 

示例

下面的示例显示 Arial 字体系列的正则样式的度量值。该代码还创建Font具有大小为 16 像素和显示的度量值 (以像素为单位) 对该特定对象 (基于 Arial 系列)Font对象。

下图显示示例代码的输出。

请注意在上图中的前两个行。 Font对象返回的大小为 16,和FontFamily对象返回的 em 高度为 2048 这两个数字 204816 是字体设计单位和实际单位 (在本例中为像素) 之间进行转换的关键Font对象。

例如,你可以将转换上升从设计单位为像素,如下所示:

以下代码通过设置PointF对象的Y坐标换行。 每次增加 y 坐标font.Height的高度特定Font对象的Height属性返回值和行间距(line spacing )(以像素为单位)一致 在此示例中,数值Height 19 请注意,这是通过将行间距度量值转换为像素(向上舍入为整数,实际为18.3984)得到的相同。

请注意, em height(也称为大小或 em 大小) 不是AscentDescent的总和。 上升和下降的总和称为单元格的高度(Cell Height) 减去内部间隙(internal leading)的单元格高度等于全身高度。 单元格高度加外部间隙(external leading)等于行间距。

 

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);

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值