Printing Graphics in Windows Forms

Printing Graphics in Windows Forms

Visual Studio .NET 2003

Frequently, you will want to print graphics in your Windows application. The Graphics class provides methods for drawing objects to a device, such as a screen or printer.

To print graphics

  1. Add a PrintDocument component to your form.
  2. Right-click your form and choose View Code.
  3. In the PrintPage event handler, use the Graphics property of the PrintPageEventArgs class to instruct the printer on what kind of graphics to print.

    In the example below, the event handler is used to create a blue ellipse within a bounding rectangle with the following location and dimensions: beginning at 100, 150 with a width of 250 and a height of 250.

    ' Visual Basic
    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
       e.Graphics.FillEllipse(Brushes.Blue, New Rectangle(100, 150, 250, 250))
    End Sub
    // C#
    private void printDocument1_PrintPage(object sender, 
    System.Drawing.Printing.PrintPageEventArgs e)
         new Rectangle(100, 150, 250, 250));
    // C++
       System::Void printDocument1_PrintPage(System::Object *  sender,
          System::Drawing::Printing::PrintPageEventArgs *  e)
             Rectangle(100, 150, 250, 250));
    Visual C# and Visual C++ Note   Be sure that the necessary code to enable the event handler is present. In this case, it would be similar to the following:
    // C#
    this.printDocument1.PrintPage += new
    // C++
    this->printDocument1->PrintPage += new
    (this, printDocument1_PrintPage);

See Also

Graphics Class | Brush Class | Windows Forms Print Support | Windows Forms

© 2016 Microsoft