【发布时间】:2014-12-30 08:55:06
【问题描述】:
我正在尝试弄清楚如何在PrintDocument 和特定尺寸的表格上绘制形状(以英寸为单位)。我对PrintDocument的DPI感到困惑
这是我开始的:
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);
}
这对于来自OnPaint 函数的Graphics 对象很有效,但打印输出错误。我做了一些调试,发现 PrintPage 事件中的 Graphics 对象的 DPI 很大(运行代码时为 600),但页面的边界为 850 x 1100。所以我尝试使用 100 的 DPI打印时使用图形的 DPI 绘制到表单时。这非常有效。
我不明白PrintDocument 如何处理 DPI。即使PrintPage 事件中的Graphics 对象表示它具有不同的 DPI,PrintDocument 是否始终具有 100 的真实 DPI?如果我在绘制到表单时假设为 96dpi,而在打印时假设为 100dpi,我会遇到问题吗?
【问题讨论】: