Share via


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

更新 : 2007 年 11 月

GDI+ を使用すると、アプリケーションにファイルとして存在するイメージをレンダリングできます。イメージをレンダリングするには、Image クラスの新しいオブジェクト (たとえば Bitmap) を作成します。次に、使用する描画サーフェイスを参照する Graphics オブジェクトを作成し、Graphics オブジェクトの DrawImage メソッドを呼び出します。イメージは、グラフィックス クラスによって指定された描画サーフェイス上に描画されます。イメージ エディタを使用して、デザイン時にイメージ ファイルを作成および編集し、実行時に GDI+ を使ってそれらをレンダリングできます。詳細については、「イメージ エディタ」を参照してください。

GDI+ を使用してイメージをレンダリングするには

  1. 表示するイメージを表すオブジェクトを作成します。このオブジェクトは、Bitmap または Metafile などの、Image を継承するクラスのメンバである必要があります。次に例を示します。

    ' Uses the System.Environment.GetFolderPath to get the path to the 
    ' current user's MyPictures folder.
    Dim myBitmap as New Bitmap _
       (System.Environment.GetFolderPath _
          (System.Environment.SpecialFolder.MyPictures))
    
    // Uses the System.Environment.GetFolderPath to get the path to the 
    // current user's MyPictures folder.
    Bitmap myBitmap = new Bitmap
       (System.Environment.GetFolderPath
          (System.Environment.SpecialFolder.MyPictures));
    
    // Uses the System.Environment.GetFolderPath to get the path to the 
    // current user's MyPictures folder.
    Bitmap^ myBitmap = gcnew Bitmap
       (System::Environment::GetFolderPath
          (System::Environment::SpecialFolder::MyPictures));
    
  2. 使用する描画サーフェイスを表す Graphics オブジェクトを作成します。詳細については、「方法 : 描画する Graphics オブジェクトを作成する」を参照してください。

    ' Creates a Graphics object that represents the drawing surface of 
    ' Button1.
    Dim g as Graphics = Button1.CreateGraphics
    
    // Creates a Graphics object that represents the drawing surface of 
    // Button1.
    Graphics g = Button1.CreateGraphics();
    
    // Creates a Graphics object that represents the drawing surface of 
    // Button1.
    Graphics^ g = button1->CreateGraphics();
    
  3. イメージをレンダリングするグラフィックス オブジェクトの DrawImage を呼び出します。描画対象のイメージだけでなく、描画対象となる場所の座標も指定する必要があります。

    g.DrawImage(myBitmap, 1, 1)
    
    g.DrawImage(myBitmap, 1, 1);
    
    g->DrawImage(myBitmap, 1, 1);
    

参照

処理手順

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

方法 : Windows フォームにテキストを描画する

線または閉じた図形の描画

概念

GDI+ でのペン、直線、および四角形

参照

イメージ エディタ

その他の技術情報

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

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