Print at full scale in C#

[Print at full scale]

This example shows how you can print at full scale so objects drawn on the screen are the same size when printed. Note that your screen and printer are probably not calibrated exactly so the result will not match perfectly. It should be fairly close, however.

The example Draw and move line segments in C# shows how you can draw and move lines on the screen. This example is similar except it adds a File menu with Print Preview and Print commands.

This example manages its line segments in the same was as the previous example. It does make one change to the drawing code, however. Rather than making the Paint event handler draw the lines directly, this version moves the drawing code into a new DrawLines method> The following code shows the idea.

// Draw the lines.
private void picCanvas_Paint(object sender, PaintEventArgs e)

// Draw the lines.
private void DrawLines(Graphics gr)

Now the program can use the same drawing method to provide a print preview or print.

The really interesting part of the program is the following PrintPage event handler, which generates printing commands for a preview or print out.

// Print the lines.
private void pdocLines_PrintPage(object sender, PrintPageEventArgs e)
    // Get the screen's resolution.
    float dpix, dpiy;
    using (Graphics gr = this.CreateGraphics())
        dpix = gr.DpiX;
        dpiy = gr.DpiY;

    // Make the page scale from printer
    // resolution to hundredths of inches.
    e.Graphics.ScaleTransform(100f / dpix, 100f / dpiy);

    // Draw the lines on the page.

    // We're done.
    e.HasMorePages = false;

Normally screen coordinates are measured in pixels but printer coordinates are measured in hundredths of inches. To print at the correct scale, the program must convert from screen coordinates to printer coordinates.

To do that, it first creates a new Graphics object on the screen. Because the for (this) is on the screen, its CreateGraphics method returns an object representing the screen. The code then uses that object’s DpiX and DpiY properties to get the number of dots (pixels) per inch on the screen. (This is typically 96 dots per inch.)

Next the code gives the printing Graphics object a scale transformation to convert from dots on the screen to hundredths of inches. For example, the scale factor in the X direction divides by dpix to convert from dots (pixels) to inches. Then multiplying by 100 converts from inches to hundredths of inches.

After it sets the Graphics object’s scale transformation, the code simply calls DrawLines as before and the printout is drawn at a scale that matches the screen.

Note that the scale transformation applies to line thicknesses, too. The example uses stock pens such as Pens.Blue and Pens.Black to draw its lines. The stock pens have thickness 1 so when they are printed the lines have thickness approximately 1/96th of an inch thick.

If you draw the lines with thickness 0, that tells the GDI+ drawing system to draw the lines as thinly as possible. On the printer, that will probably produce a very thin line.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in drawing, GDI+, graphics, printing and tagged , , , , , , , , , , , , , , , , . Bookmark the permalink.

6 Responses to Print at full scale in C#

  1. John says:

    Hi Rod,
    I have tried to draw a rectangle at a certain size(20mm x 80mm) with
    gr.DrawRectangle(new Pen(Brushes.Red, 0.5f), 20,20, 20, 80);
    in Drawlines and
    e.Graphics.ScaleTransform(100f / dpix, 100f / dpiy);
    in printpage but it comes out wrong size but if i delete
    e.Graphics.ScaleTransform(100f / dpix, 100f / dpiy);
    and use
    gr.PageUnit = GraphicsUnit.Millimeter;
    in drawlines it is the right size, but it seems to mess up the mouse location x,y. is there anyway of rectifying this so i can use the rectangle.contains accurately.

    sorry for not explaining properly, i am not a very good programmer.
    Thank you

    • RodStephens says:

      This example draws in inches not millimeters. Try using this:

      e.Graphics.ScaleTransform(25.4f / dpix, 25.4f / dpiy);

      That should convert screen units into millimeters.

  2. Jeff Kennedy says:

    Hey, Rod, I am trying to use this example to print a series of rectangles that are drawn but for some reason I am only getting one of the rectangles it is as if it isnt scaling at all.

    Here is my code for drawing the rectangles.

    private void CreateGraph(Graphics graphics, int width, int height, int days)
        var rectStartX = 0;
        var rectStartY = 0;
        Font f = new Font("Arial", 16, FontStyle.Bold);
        penWidth = graphPen.Width;
        StringFormat sf = new StringFormat
            Alignment = StringAlignment.Center,        // horizontal alignment
            LineAlignment = StringAlignment.Center    // vertical alignment
        using (var g = graphics)
            //Create rectangles based number of days.
            for (int i = 0; i < days; i++)
                var rect = new Rectangle(rectStartX, rectStartY, width, height);
                rectStartX += widthOfOneDay;
                string s = Convert.ToString(i + 1);
                //g.DrawRectangle(myPen, rect);
                DrawRectangle(g, rect, penWidth);
                g.DrawString(s, f, Brushes.Black, rect, sf);

    Any help would be awesome. I have never worked with any of this but I have a requirement for it.

  3. RodStephens says:

    You are setting up the ScaleTransform, right? If not, then the units may be too small because printer units are hundredths of inches.

    I would set a breakpoint inside the loop and see what the rectangle’s values are. If that doesn’t work, email me the project (zipped) and I’ll take a look.

    • Jeff Kennedy says:

      I was but I couldn’t seem to get it to work. I have never worked with gdi, I am a .net backend developer, but I am trying to help someone with a project here at work. Where so I find your email address?

      Thanks so much.

Comments are closed.