Share via


HOW TO:建立繪製的圖形物件

更新:2007 年 11 月

在可使用 GDI+ 來描繪線條和形狀、呈現文字或顯示和管理影像之前,您必須先建立 Graphics 物件。Graphics 物件代表的是 GDI+ 繪圖介面,而且是用來建立圖形影像的物件。

使用圖形的兩個步驟如下:

  1. 建立 Graphics 物件。

  2. 使用 Graphics 物件來繪製線條和形狀、呈現文字或顯示和管理影像。

建立圖形物件

您可使用下列幾種方法來建立圖形物件:

若要建立圖形物件

  • 在表單或控制項的 Paint 事件中,接收圖形物件的參考做為 PaintEventArgs 的一部分。這通常是當您建立控制項的描繪程式碼時,取得圖形物件參考的方式。

    -或-

  • 藉由呼叫控制項或表單的 CreateGraphics 方法來取得代表該控制項或表單之繪圖介面的 Graphics 物件參考。如果您想要在現有表單或控制項上進行繪圖,請使用這個方法。

    -或-

  • 從繼承自 Image 的任何物件建立 Graphics 物件。當您要變更現有影像時,這個方法會相當有用。

    下列各節將詳細說明每一個處理程序。

Paint 事件處理常式中的 PaintEventArgs

為控制項的 PaintEventHandler 設計程式時,會提供圖形物件做為其中一個 PaintEventArgs

若要從 Paint 事件中的 PaintEventArgs 取得 Graphics 物件的參考

  1. 宣告 Graphics 物件。

  2. 指派變數,以參考當成 PaintEventArgs 一部分傳遞的 Graphics 物件。

  3. 插入程式碼來繪製表單或控制項。

下列範例顯示如何參考 Paint 事件中來自 PaintEventArgsGraphics 物件:

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 方法建立 Graphics 物件

  • 請呼叫要呈現圖形所在表單或控制項的 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 物件建立

除此之外,您可從衍生自 Image 類別的任何物件建立圖形物件。

若要從 Image 建立 Graphics 物件

下列範例顯示如何使用 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 檔案建立 Graphics 物件,例如 16 位元、24 位元和 32 位元的 .bmp 檔。未索引 .bmp 檔案的每個像素都只包含一種色彩,相較於已索引 .bmp 檔的像素,後者則是包含了色彩表索引。

描繪與管理形狀和影像

建立 Graphics 物件之後,您可使用它來繪製線條和形狀、呈現文字或顯示和管理影像。與 Graphics 物件搭配使用的主要物件如下:

  • Pen 類別—用於描繪線條、勾畫形狀或是呈現其他的幾何圖形。

  • Brush 類別—用於填滿圖形區域,例如實心形狀、影像或文字。

  • Font 類別—提供呈現文字時使用哪種形狀的描述。

  • Color 結構—代表要顯示的不同色彩。

若要使用已建立的圖形物件

請參閱

工作

HOW TO:使用 GDI+ 呈現影像

其他資源

圖形程式設計入門

Windows Form 中的圖形和繪圖

線條、曲線和形狀