共用方式為


HOW TO:使用 GDI+ 呈現影像

更新:2007 年 11 月

您可以使用 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 物件,代表所要使用的繪圖介面。如需詳細資訊,請參閱 HOW TO:建立繪製的圖形物件

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

請參閱

工作

HOW TO:建立繪製的圖形物件

HOW TO:在 Windows Form 上繪製文字

繪製線條或封閉圖形

概念

GDI+ 中的畫筆、線條和矩形

參考

影像編輯器

其他資源

圖形程式設計入門

Windows Form 中的圖形和繪圖