PrintPageEventArgs.Graphics Property
Gets the Graphics used to paint the page.
[Visual Basic] Public ReadOnly Property Graphics As Graphics [C#] public Graphics Graphics {get;} [C++] public: __property Graphics* get_Graphics(); [JScript] public function get Graphics() : Graphics;
Property Value
The Graphics used to paint the page.
Example
[Visual Basic, C#, C++] Use the System.ComponentModel, System.Collections, System.Drawing, System.Drawing.Printing, System.Resources, and System.Windows.Forms namespaces for this example.
[Visual Basic, C#, C++] The following example assumes a Button has been created on a Form. The printButton_Click method from the example creates an instance of PrintDocument, calls the pd_PrintPage method, 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.
[Visual Basic] ' Specifies what happens when the user clicks the Button. Private Sub printButton_Click(sender As Object, e As EventArgs) Try ' Assumes the default printer. Dim pd As New PrintDocument() AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage 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) ' 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 [C#] // Specifies what happens when the user clicks the Button. private void printButton_Click(object sender, EventArgs e) { try { // Assumes the default printer. PrintDocument pd = new PrintDocument(); pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); pd.Print(); } catch(Exception ex) { MessageBox.Show("An error occurred while printing", ex.ToString()); } } // Specifies what happens when the PrintPage event is raised. private void pd_PrintPage(object sender, PrintPageEventArgs ev) { // 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; } [C++] // Specifies what happens when the user clicks the Button. private: void printButton_Click(Object* /*sender*/, EventArgs* /*e*/) { try { // Assumes the default printer. PrintDocument* pd = new PrintDocument(); pd->PrintPage += new PrintPageEventHandler(this, &Form1::pd_PrintPage); pd->Print(); } catch(Exception* ex) { MessageBox::Show(S"An error occurred while printing", ex->ToString()); } } // Specifies what happens when the PrintPage event is raised. private: void pd_PrintPage(Object* /*sender*/, PrintPageEventArgs* ev) { // Draw a picture. ev->Graphics->DrawImage(Image::FromFile(S"C:\\My Folder\\MyFile.bmp"), ev->Graphics->VisibleClipBounds); // Indicate that this is the last page to print. ev->HasMorePages = false; }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
PrintPageEventArgs Class | PrintPageEventArgs Members | System.Drawing.Printing Namespace | Graphics