完成以下代码后,“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;
}