2

完成以下代码后,“result1”和“result2”变量(字符串的测量宽度)的值是相同的,尽管“font1”是常规的,“font2”是粗体的。有趣的是,字体“Times New Roman”和“Arial”会出现此错误。例如对于字体“Calibri”返回值是正确的,变量“result2”的值大于变量“result1”的值。为什么会这样?

using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromHwnd(IntPtr.Zero))
{
    graphics.PageUnit = System.Drawing.GraphicsUnit.Pixel;
    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    System.Drawing.Font font1 = new System.Drawing.Font("Arial", 10.0f, System.Drawing.FontStyle.Regular);
    System.Drawing.Font font2 = new System.Drawing.Font("Arial", 10.0f, System.Drawing.FontStyle.Bold);
    float result1 = graphics.MeasureString("WWW", font1, int.MaxValue, System.Drawing.StringFormat.GenericTypographic).Width;
    float result2 = graphics.MeasureString("WWW", font2, int.MaxValue, System.Drawing.StringFormat.GenericTypographic).Width;
}
4

2 回答 2

3

实际上在 OnPaint() 方法中绘制它,你会明白为什么:

在此处输入图像描述

于 2011-11-07T18:25:02.460 回答
2

我只是用不同的字符串尝试过,宽度确实发生了变化。我认为恰好“WWW”的长度相同,有/没有粗体样式。只需在编辑器中尝试一下,您就会看到它的大小相同。

于 2011-11-07T18:27:54.977 回答