我的打印表单包含许多打印位置。我想将文本框文本传递给打印文档中的正确位置。我正在使用(像素)来提及打印点。
g.DrawString("Total", new Font("Arial", 10, FontStyle.Regular), Brushes.Black, new Point(40, 160));
g.DrawString(": ", new Font("Arial", 10, FontStyle.Regular), Brushes.Blue, new Point(150, 160));
测量值以cm为单位,并转换为像素。但它没有放在正确的位置。
我有什么选择可以更好地控制测量单位?
答案 0 :(得分:3)
Graphics实例上PageUnit
的默认设置为Display
指定显示设备的度量单位。通常是视频显示的像素,打印机的1/100英寸。
因此,在不更改任何内容的情况下,PrinterDocument的Graphics实例上的Point描绘了1/100英寸。
有了这个信息,一个可能的解决方案是引入辅助方法,计算英寸到1/100英寸和厘米到英寸。
public static PointF FromCm(float cmx, float cmy)
{
const float cm2inch = 1/2.54F;
return FromInch(cmx * cm2inch, cmy * cm2inch);
}
public static PointF FromInch(float inchx, float inchy)
{
return new PointF(inchx * 100, inchy * 100);
}
并像这样使用它:
g.DrawString(
"Total",
new Font("Arial", 10, FontStyle.Regular),
Brushes.Black,
FromCm(1,1));
GraphicsUnit枚举确实有Millimeter
的设置。
g.PageUnit = GraphicsUnit.Millimeter;
g.DrawString(
"Total",
new Font("Arial", 10, FontStyle.Regular),
Brushes.Black,
new PointF(25 , 10)); // due to millimeter
// setting this is now 2,5 cm and 1 cm
您可以在绘画过程中随时切换PageUnit
。所以你可以混合搭配任何测量单位都很方便。