Share via


方法 : 描画する Graphics オブジェクトを作成する

更新 : 2007 年 11 月

GDI+ を使用して線や形状の描画、テキストのレンダリング、イメージの表示と操作などを行うには、Graphics オブジェクトを作成する必要があります。Graphics オブジェクトは、GDI+ の描画サーフェイスを表し、グラフィカル イメージの作成に使用します。

グラフィックスの操作には 2 つの手順があります。

  1. Graphics オブジェクトを構築する。

  2. Graphics オブジェクトを使用して、線や形状の描画、テキストのレンダリング、またはイメージの表示と操作を実行する。

Graphics オブジェクトの作成

グラフィックス オブジェクトはさまざまな方法で作成できます。

グラフィックス オブジェクトを作成するには

  • グラフィックス オブジェクトへの参照を、フォームまたはコントロールの Paint イベントの PaintEventArgs の一部として受け取ります。これは、コントロールの描画コードを作成するときにグラフィックス オブジェクトへの参照を取得するための一般的な方法です。

    または

  • コントロールまたはフォームの CreateGraphics メソッドを呼び出して、そのコントロールまたはフォームの描画サーフェイスを表す Graphics オブジェクトへの参照を取得します。このメソッドは、既に存在するフォームまたはコントロール上に描画する場合に使用します。

    または

  • Image を継承する任意のオブジェクトから Graphics オブジェクトを作成します。この方法は、既存のイメージを変更する場合に便利です。

    これらのそれぞれの方法について、以降で詳しく説明します。

Paint イベント ハンドラの PaintEventArgs

コントロールの PaintEventHandler をプログラミングする場合、グラフィックス オブジェクトが PaintEventArgs の 1 つとして用意されています。

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 メソッドで 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 オブジェクトの作成元となる Image 変数の名前を指定して、Graphics.FromImage メソッドを呼び出します。

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);
メモ :

Graphics オブジェクトを作成できるのは、16 ビット .bmp ファイル、24 ビット .bmp ファイル、32 ビット .bmp ファイルなどの、インデックスのない .bmp ファイルからだけです。インデックスのない .bmp ファイルの各ピクセルは色を保持します。これに対して、インデックス付き .bmp ファイルのピクセルはカラー テーブルに対するインデックスを保持します。

形状およびイメージの描画と操作

作成した Graphics オブジェクトを使用して、線や形状の描画、テキストのレンダリング、およびイメージの表示と操作を行うことができます。Graphics オブジェクトと一緒に使用される主要なオブジェクトを次に示します。

  • Pen クラス - 線の描画、形状のアウトラインの作成、またはその他の幾何学表現に使用します。

  • Brush クラス - 形状、イメージ、テキストの塗りつぶしなど、グラフィック領域の塗りつぶしに使用します。

  • Font クラス - テキストのレンダリング時に使用する形状についての説明を提供します。

  • Color 構造体 - 各種の表示色を表します。

作成した Graphics オブジェクトを使用するには

参照

処理手順

方法 : GDI+ を使用してイメージをレンダリングする

その他の技術情報

グラフィックス プログラミングについて

Windows フォームにおけるグラフィックスと描画

直線、曲線、および図形