Print a form or control in C#

[Print a form or control]

This example shows how you can print a form or control in a C# program. Way back in the days of Visual Basic 6, there was a PrintForm method. It simply grabbed an image of the form and printed it. It wasn’t fancy, but it was much easier than drawing a printout, so many programs used it as their initial printing method. (And many programs never got around to making a new method for printing.)

Unfortunately .NET doesn’t have a PrintForm method so there’s no really easy way to print a form’s image. This example shows how you can grab the image of a form or control and display it in a print preview dialog. You can use the dialog to print it, or if you prefer you can send the printout directly to the printer by calling the PrintDocument object’s Print method.

For information on how to grab the image of a form or control, see the post Get the image of a control or form, or a form’s client area in C#.

This example uses the following code to display the image of a control (including the form) in the print preview dialog.

private void ShowControlImage(Control ctl)
{
    using (Bitmap bm = GetControlImage(ctl))
    {
        PrintImage(bm);
    }
}

This code calls GetControlImage (which uses the previous post’s techniques) to grab the control’s image. It then calls the following PrintImage method to display the print preview.

// Send the image to a PrintPreviewDialog.
// (You could modify it to print directly.)
private Image ImageToPrint;
private void PrintImage(Image image)
{
    // Save a reference to the image to print.
    ImageToPrint = image;

    // Display the dialog.
    ppdForm.ShowDialog();
}

This code defines the ImageToPrint variable. The PrintImage method saves the image it should display in that variable and then displays the ppdForm PrintPreviewDialog component.

At design time, I created the PrintPreviewDialog component and a PrintDocument object. I also set the dialog’s Document property equal to the PrintDocument so it would be able to display the document when it was shown.

The last piece to the example is the following PrintPage event handler.

// Print the page.
private void pdocForm_PrintPage(object sender, PrintPageEventArgs e)
{
    // Center the image.
    int cx = e.MarginBounds.X + e.MarginBounds.Width / 2;
    int cy = e.MarginBounds.Y + e.MarginBounds.Height / 2;
    Rectangle rect = new Rectangle(
        cx - ImageToPrint.Width / 2, cy - ImageToPrint.Height / 2,
        ImageToPrint.Width, ImageToPrint.Height);

    e.Graphics.InterpolationMode = InterpolationMode.High;
    e.Graphics.DrawImage(ImageToPrint, rect);
}

When the PrintDocument object needs to print something, it raises the PrintPage event. The event handler calculates the position it needs to give the image stored in the variable ImageToPrint so the image is centered within the printed page’s margins. It then makes a Rectangle to define where the image should be printed.

Next the code sets the e.Graphics object’s InterpolationMode to produce a slightly smoother image. Finally the code calls DrawImage method to draw the image on the printed page.

The whole process is a bit convoluted because of the way C# prints, but there’s actually not too much code.


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 controls, forms, printing and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *