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

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

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

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

    ' 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);  
    

関連項目