我正在做的是获取字符串的像素大小并将其转换为百分之一英寸(即像素/DPI = 英寸,英寸 * 100 = 百分之一英寸)。这是我的代码:
private static SizeF TextSize(string text, Font txtFnt)
{
SizeF txtSize = new SizeF();
// The size returned is 'Size(int width, int height)' where width and height
// are the dimensions of the string in pixels
Size s = System.Windows.Forms.TextRenderer.MeasureText(text, txtFnt);
// Value based on normal DPI settings of 96
txtSize.Width = (float)Math.Ceiling((float)s.Width / 96f * 100f);
txtSize.Height = (float)Math.Ceiling((float)s.Height / 96f * 100f);
return txtSize;
}
现在,使用 Arial 字体,这对于小于 12 的字体都可以正常工作,但之后字符开始被截断,因为计算的大小小于实际大小。我知道我的 DPI 设置为 96。我的字体都定义为相同的字体大小变化:
Font myFont = new Font("Arial", <font size>, FontStyle.Regular, GraphicsUnit.Point);
我相信我必须使用GraphicsUnit.Point
自定义控件,因为我正在绘制字符串,但有GraphicsUnit
关系吗?
该MeasureText
功能是否正常工作,或者还有其他事情发生?
编辑
我正在绘制自定义打印预览控件。打印预览控件中的单位是“英寸/100”(因此进行了转换)。我相信文本、图像等是用打印机图形对象绘制的,但我并不完全确定。