如何:建立繪製的圖形物件

您必須先建立 Graphics 物件,才能使用 GDI+繪製線條和圖形、轉譯文字或顯示及操作影像。 物件 Graphics 代表 GDI+ 繪圖介面,而 是用來建立圖形影像的物件。

使用圖形有兩個步驟:

  1. 建立 Graphics 物件。

  2. Graphics使用 物件繪製線條和圖形、轉譯文字或顯示及操作影像。

建立繪圖物件

繪圖物件可以透過各種方式建立。

建立繪圖物件

  • 在表單或控制項的 事件中 Paint ,接收繪圖物件的 PaintEventArgs 參考。 這通常是您在為控制項建立繪製程式碼時,取得繪圖物件的參考。 同樣地,您也可以在處理 PrintPage 的 事件 PrintDocument 時,取得繪圖物件做為 的 屬性 PrintPageEventArgs

    -或-

  • CreateGraphics呼叫控制項或表單的 方法,以取得物件參照 Graphics ,該物件表示該控制項或表單的繪圖介面。 如果您想要在已經存在的表單或控制項上繪製,請使用此方法。

    -或-

  • Graphics從繼承自 Image 的任何 物件建立 物件。 當您想要改變已經存在的映射時,此方法很有用。

    下列各節提供每個程式的詳細資料。

小畫家 事件處理常式中的 小畫家EventArgs

當針對 PaintEventHandler 控制項或 PrintDocumentPrintPage 的 進行程式設計時,繪圖物件會做為 或 PrintPageEventArgsPaintEventArgs 其中一個屬性。

若要從 小畫家 事件中的 小畫家EventArgs 取得 Graphics 物件的參考

  1. Graphics宣告 物件。

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

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

    下列範例示範如何從 PaintEventArgs 事件中 Paint 參考 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 方法建立 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 物件

  • 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 檔案建立 Graphics 物件,例如 16 位、24 位和 32 位 .bmp 檔案。 非索引 .bmp 檔案的每個圖元都會保留色彩,與索引的 .bmp 檔案圖元相反,該圖元會保存色彩資料表的索引。

繪製和操作圖形和影像

建立物件之後, Graphics 可以用來繪製線條和圖形、轉譯文字或顯示及操作影像。 與 物件搭配 Graphics 使用的主體物件為:

  • 類別 Pen : 用於繪製線條、大綱圖形,或轉譯其他幾何表示。

  • 類別 Brush : 用於填滿圖形的區域,例如填滿圖形、影像或文字。

  • 類別 Font - 提供轉譯文字時要使用的圖形描述。

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

若要使用您已建立的 Graphics 物件

另請參閱