如何:为绘图创建图形对象

在使用 GDI+ 绘制线条和形状、呈现文本或显示和操作图像之前,需要创建 Graphics 对象。 Graphics 对象表示 GDI+ 绘图图面,并且是用于创建图形图像的对象。

使用图形有两个步骤:

  1. 创建 Graphics 对象。

  2. 使用 Graphics 对象绘制线条和形状、呈现文本或显示和操作图像。

创建图形对象

可以通过多种方式创建图形对象。

创建 Graphics 对象

  • 接收对 Graphics 对象的引用,作为窗体或控件的 Paint 事件中 PaintEventArgs 的一部分。 通常,在为控件编写绘图代码时,这是获取图形对象引用的方式。 同样,你也可以在处理 PrintDocumentPrintPage 事件时获取 Graphics 对象作为 PrintPageEventArgs 的属性。

    - 或 -

  • 调用控件或窗体的 CreateGraphics 方法以获取对表示该控件或窗体绘图图面的 Graphics 对象的引用。 如果要在已存在的窗体或控件上绘制,请使用此方法。

    - 或 -

  • 从继承自 Image的任何对象创建 Graphics 对象。 如果要更改已存在的映像,此方法非常有用。

    以下部分详细介绍了其中每个过程。

Paint 事件处理程序中的 PaintEventArgs

在为控件编程 PaintEventHandler 或为 PrintDocument编程 PrintPage 时,图形对象是 PaintEventArgsPrintPageEventArgs的属性之一。

从 Paint 事件中的 PaintEventArgs 获取对 Graphics 对象的引用

  1. 声明 Graphics 对象。

  2. 分配变量以引用作为 PaintEventArgs 的一部分传递的 Graphics 对象。

  3. 插入代码以绘制窗体或控件。

    以下示例演示如何从 Paint 事件中的 PaintEventArgs 引用 Graphics 对象:

    Private Sub Form1_Paint(sender As Object, pe As PaintEventArgs) Handles _  
       MyBase.Paint  
       ' Declares the Graphics object and sets it to the Graphics object  
       ' supplied in the PaintEventArgs.  
       Dim g As Graphics = pe.Graphics  
       ' Insert code to paint the form here.  
    End Sub  
    
    private void Form1_Paint(object sender,
       System.Windows.Forms.PaintEventArgs pe)
    {  
       // Declares the Graphics object and sets it to the Graphics object  
       // supplied in the PaintEventArgs.  
       Graphics g = pe.Graphics;  
       // Insert code to paint the form here.  
    }  
    
    private:  
       void Form1_Paint(System::Object ^ sender,  
          System::Windows::Forms::PaintEventArgs ^ pe)  
       {  
          // Declares the Graphics object and sets it to the Graphics object  
          // supplied in the PaintEventArgs.  
          Graphics ^ g = pe->Graphics;  
          // Insert code to paint the form here.  
       }  
    

CreateGraphics 方法

还可以使用控件或窗体的 CreateGraphics 方法获取对表示该控件或窗体绘图图面的 Graphics 对象的引用。

使用 CreateGraphics 方法创建图形对象

  • 在你要呈现图形的窗体或控件上调用 CreateGraphics 方法。

    Dim g as Graphics  
    ' Sets g to a Graphics object representing the drawing surface of the  
    ' control or form g is a member of.  
    g = Me.CreateGraphics  
    
    Graphics g;  
    // Sets g to a graphics object representing the drawing surface of the  
    // control or form g is a member of.  
    g = this.CreateGraphics();  
    
    Graphics ^ g;  
    // Sets g to a graphics object representing the drawing surface of the  
    // control or form g is a member of.  
    g = this->CreateGraphics();  
    

从图像对象创建

此外,还可以从派生自 Image 类的任何对象创建图形对象。

从图像创建图形对象

  • 调用 Graphics.FromImage 方法,提供要从中创建 Graphics 对象的 Image 变量的名称。

    以下示例演示如何使用 Bitmap 对象:

    Dim myBitmap as New Bitmap("C:\Documents and Settings\Joe\Pics\myPic.bmp")  
    Dim g as Graphics = Graphics.FromImage(myBitmap)  
    
    Bitmap myBitmap = new Bitmap(@"C:\Documents and
       Settings\Joe\Pics\myPic.bmp");  
    Graphics g = Graphics.FromImage(myBitmap);  
    
    Bitmap ^ myBitmap = gcnew  
       Bitmap("D:\\Documents and Settings\\Joe\\Pics\\myPic.bmp");  
    Graphics ^ g = Graphics::FromImage(myBitmap);  
    

说明

只能从非索引 .bmp 文件(例如 16 位、24 位和 32 位 .bmp 文件)创建 Graphics 对象。 非索引 .bmp 文件的每个像素都保存一种颜色,而索引 .bmp 文件的像素保存颜色表的索引。

绘制和操作形状和图像

创建后,可以使用 Graphics 对象绘制线条和形状、呈现文本或显示和操作图像。 与 Graphics 对象一起使用的主体对象包括:

  • Pen 类 - 用于绘制线条、勾勒形状或呈现其他几何图形。

  • Brush 类 - 用于填充图形区域,如填充形状、图像或文本。

  • Font 类 - 提供呈现文本时要使用的形状的说明。

  • Color 结构 - 表示要显示的不同颜色。

使用已创建的 Graphics 对象

另请参阅