我试图弄清楚如何在aPrintDocument
和特定尺寸(以英寸为单位)上绘制形状。我对DPI感到困惑PrintDocument
这是我开始的:
private void DrawShapes(Graphics graphics)
{
graphics.DrawRectangle(new Pen(Color.HotPink), new Rectangle(0, 0, (int)Math.Round(1 * graphics.DpiX), (int)Math.Round(1 * graphics.DpiY)));
graphics.DrawRectangle(new Pen(Color.HotPink), new Rectangle(0, 0, (int)Math.Round(1.5 * graphics.DpiX), (int)Math.Round(1.5 * graphics.DpiY)));
graphics.DrawRectangle(new Pen(Color.HotPink), new Rectangle(0, 0, (int)Math.Round(2 * graphics.DpiX), (int)Math.Round(2 * graphics.DpiY)));
graphics.DrawRectangle(new Pen(Color.HotPink), new Rectangle(0, 0, (int)Math.Round(2.5 * graphics.DpiX), (int)Math.Round(2.5 * graphics.DpiY)));
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawShapes(e.Graphics);
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
DrawShapes(e.Graphics);
}
这对于函数中的Graphics
对象效果很好,OnPaint
但是打印输出错误。我进行了一些调试,发现事件中Graphics
对象的DPIPrintPage
很大(运行代码时为600),但是页面的边界是850 x1100。所以我尝试在打印时使用100的DPI并使用图形'DPI绘制到窗体时。这工作得很好。
我不了解PrintDocument
与DPI的交易情况。是否PrintDocument
总是具有100 DPI如此,即使Graphics
从对象PrintPage
的事件表示,它有不同的DPI?如果在绘制到表格时假定96dpi,在打印时假定100dpi,是否会遇到问题?
您在PrintPage事件处理程序中获得的Graphics对象是使用Graphics.PageUnit属性设置为初始化的GraphicsUnit.Display
。这样可以确保将输出缩放到任何打印机分辨率,在纸上绘制100像素长的线将是一英寸。使用300或600 dpi打印机都没有关系。请注意,您的850 x 1100纸张尺寸是8.5 x 11英寸,在美国是标准尺寸。
该选择并非偶然,它接近显示器的默认dpi。因此,绘制到屏幕上的代码也可以用于绘制到打印机上。即使打印机具有更高的分辨率,纸上的尺寸也将大致相同。几乎没有理由更改此设置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句