Home > OS >  Is the unit in Windows Forms printing always 100dpi point?
Is the unit in Windows Forms printing always 100dpi point?

Time:03-18

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);
}

CodePudding user response:

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.

  • Related