【问题标题】:Printing Resolution打印分辨率
【发布时间】: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,我会遇到问题吗?

【问题讨论】:

    标签: c# printing


    【解决方案1】:

    您在 PrintPage 事件处理程序中获得的 Graphics 对象已使用设置为 GraphicsUnit.Display 的 Graphics.PageUnit 属性进行初始化。这确保您的输出可以缩放到打印机分辨率,您绘制的 100 个“像素”长的线在纸上将是一英寸。使用 300 或 600 dpi 的打印机并不重要。请注意,您的 850 x 1100 纸张尺寸是 8.5 x 11 英寸,这是美国的标准尺寸。

    这个选择不是偶然的,它接近显示器的默认 dpi。因此,绘制到屏幕的代码也可以用于绘制到打印机。即使打印机具有更高的分辨率,纸上的尺寸也将大致相同。很少有理由更改此设置。

    【讨论】:

    • 谢谢!我没有意识到图形单元设置为显示。我做了一些研究,现在它变得更有意义了。我很感激你的回答:)
    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    相关资源
    最近更新 更多