共用方式為


操作說明:使用 GDI+ 呈現影像

您可以使用 GDI+ 來轉譯應用程式中以檔案的形式存在的影像。 您可以建立 類別的新物件 Image ,例如 Bitmap ,建立 Graphics 參照您要使用的繪圖介面的物件,並呼叫 DrawImage 物件的 方法 Graphics 。 影像將繪製於此圖形類別所代表的繪圖介面上。 您可以使用影像編輯器在設計階段建立和編輯影像檔,並在執行時間使用 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 物件,代表您想要使用的繪圖介面。 如需詳細資訊,請參閱如何:建立繪圖的圖形物件

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

另請參閱