Windows Forms打印的单位总是100dpi点吗?

发布于 2025-01-13 11:41:26 字数 1930 浏览 0 评论 0 原文

使用 Windows 窗体 打印时使用的坐标系单位是什么.printdocument?view=netframework-4.8" rel="nofollow noreferrer">PrintDocument 类?为了在特定位置以特定尺寸打印某些内容,需要此信息。

PrintPage 事件,PrintPageEventArgs 实例具有属性 图形PageBounds。他们似乎使用相同的坐标系。

对于 A4 纵向纸张,PageBounds 返回尺寸为 827 x 1169。假设 A4 纸张为 210mm x 297mm,单位为 Graphics / PageBounds单位似乎是像素/点,100dpi。 (827 / 210 * 25.4 = 100.0278,1169 / 297 * 25.4 = 99.9751)。

使用100dpi对对象进行缩放定位,绘制结果正确。但它总是 100dpi 吗?或者如何查询单位?

(查询Graphics.DpiX不起作用。它返回600dpi,这是打印机DPI,但不是坐标系DPI。)

private void PrintButton_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);
    pd.Print();
}

private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
    Rectangle bounds = e.PageBounds; // For A4 portrait sheet: {X = 0 Y = 0 Width = 827 Height = 1169}
    float dpi = e.Graphics.DpiX; // 600
    DrawIt(e.Graphics);
}

What is the unit of the coordinate system used in Windows Forms printing using the PrintDocument class? This information is needed in order to print something at a specific position and with a specific size.

In the PrintPage event, the PrintPageEventArgs instance has the properties Graphics and PageBounds. They seem to use the same coordinate system.

For an A4 portrait sheet, PageBounds returns a size of 827 by 1169. Given an A4 sheet is 210mm by 297mm, the unit Graphics / PageBounds unit seems to be pixels/points with 100dpi. (827 / 210 * 25.4 = 100.0278, 1169 / 297 * 25.4 = 99.9751).

Using 100dpi to scale and position the objects, the drawing result is correct. But is it always 100dpi? Or how can I query the unit?

(Querying Graphics.DpiX does not work. It returns 600dpi, which is the printer DPI but not the coordinate system DPI.)

private void PrintButton_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);
    pd.Print();
}

private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
    Rectangle bounds = e.PageBounds; // For A4 portrait sheet: {X = 0 Y = 0 Width = 827 Height = 1169}
    float dpi = e.Graphics.DpiX; // 600
    DrawIt(e.Graphics);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖粟与秋泊 2025-01-20 11:41:26

感谢吉米指出该单位是显示。简短的回答是:打印始终为 100dpi。

Graphics 实例使用 GraphicsUnit.Display 作为 PageUnit。对于打印机来说,这是1/100 英寸,即 100dpi。文档中说“通常”,但这可能指的是“视频显示”。

它还与 PrinterUnit 一致。显示,始终为 0.01 英寸。

由于图形测量值也与PageBounds,我可以放心地假设 PageBounds 和其他 PrintPageEventArgs 属性还使用 100dpi 的打印机显示单位。但它没有记录在案。

Thanks to Jimi who pointed out that the unit is Display. The short answer is: It's always 100dpi for printing.

The Graphics instance uses GraphicsUnit.Display as the PageUnit. And for printers, this is 1/100 inch for printers, i.e. 100dpi. The documentation says "typically" but this probably refers to the video displays.

It also coincides with PrinterUnit.Display, which is always 0.01in.

As the Graphics measurements are also consistent with PageBounds, I can probably safely assume that PageBounds and other PrintPageEventArgs properties also use display units for printers with 100dpi. It's not documented though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文