次の方法で共有


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

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

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

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

    ' 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 オブジェクトを作成します。 詳細については、「方法: 描画用のグラフィックス オブジェクトを作成する」を参照してください。

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

こちらも参照ください