Share via


PrintPageEventArgs.Graphics 属性

获取用于绘制页的 Graphics

**命名空间:**System.Drawing.Printing
**程序集:**System.Drawing(在 system.drawing.dll 中)

语法

声明
Public ReadOnly Property Graphics As Graphics
用法
Dim instance As PrintPageEventArgs
Dim value As Graphics

value = instance.Graphics
public Graphics Graphics { get; }
public:
property Graphics^ Graphics {
    Graphics^ get ();
}
/** @property */
public Graphics get_Graphics ()
public function get Graphics () : Graphics

属性值

用于绘制页的 Graphics

示例

下面的代码示例假定已经在 Form 上创建了一个 Button。示例中的 printButton_Click 方法创建 PrintDocument 类的一个实例,调用 pd_PrintPage 方法,然后打印 pd_PrintPage 方法中指定的 .bmp 文件。若要运行该示例,请更改要打印的位图的路径。

System.ComponentModelSystem.CollectionsSystem.DrawingSystem.Drawing.PrintingSystem.ResourcesSystem.Windows.Forms 命名空间用于此示例。

' 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
// 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;
 }
 
private:
   // Specifies what happens when the user clicks the Button.
   void printButton_Click( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      try
      {
         // Assumes the default printer.
         PrintDocument^ pd = gcnew PrintDocument;
         pd->PrintPage += gcnew PrintPageEventHandler( this, &Form1::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.
   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;
   }
// 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.add_PrintPage(new PrintPageEventHandler(this.pd_PrintPage));
        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 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

PrintPageEventArgs 类
PrintPageEventArgs 成员
System.Drawing.Printing 命名空间
Graphics 类