PrintPageEventArgs.Graphics Property

Gets the Graphics used to paint the page.

Namespace: System.Drawing.Printing
Assembly: System.Drawing (in system.drawing.dll)

'Declaration
Public ReadOnly Property Graphics As Graphics
'Usage
Dim instance As PrintPageEventArgs
Dim value As Graphics

value = instance.Graphics

/** @property */
public Graphics get_Graphics ()

public function get Graphics () : Graphics

Not applicable.

Property Value

The Graphics used to paint the page.

The following code example assumes a Button named printButton and a PrintDocument named pd have been created on a Form. Make sure the Click event for the Button is associated with the printButton_Click method and the PrintPage event of the PrintDocument is associated with the pd_PrintPage method in the example. The printButton_Click method from the example calls the Print method raising the PrintPage event, and prints the .bmp file specified in the pd_PrintPage method. To run this example, change the path to the bitmap you want to print.

Use the System.Drawing, System.Drawing.Printing, and System.Windows.Forms namespaces for this example.

' Specifies what happens when the user clicks the Button.
Private Sub printButton_Click(sender As Object, e As EventArgs) _
Handles printButton.Click
    Try
       pd.Print()
    Catch ex As Exception
        MessageBox.Show("An error occurred while printing", _
            ex.ToString())
    End Try
End Sub    

' Specifies what happens when the PrintPage event is raised.
Private Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs) _
Handles pd.PrintPage

    ' Draw a picture.
    ev.Graphics.DrawImage(Image.FromFile("C:\My Folder\MyFile.bmp"), _
        ev.Graphics.VisibleClipBounds)
    
    ' Indicate that this is the last page to print.
    ev.HasMorePages = False
End Sub



// Specifies what happens when the user clicks the Button.
 private void printButton_Click(Object sender, EventArgs e)
 {
     try {
        pd.Print();
     }
     catch (System.Exception ex) {
         MessageBox.Show("An error occurred while printing", ex.ToString());
     }
 } //printButton_Click

 // Specifies what happens when the PrintPage event is raised.
 private void pd_PrintPage(Object sender, PrintPageEventArgs ev)
 {
     // Draw a picture.
     ev.get_Graphics().DrawImage(Image.FromFile
         ("C:\\My Folder\\MyFile.bmp"),
         ev.get_Graphics().get_VisibleClipBounds());

     // Indicate that this is the last page to print.
     ev.set_HasMorePages(false);
 } //pd_PrintPage

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show: