共用方式為


操作說明:使用 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 物件來代表您要使用的繪圖介面。 如需詳細資訊,請參閱如何:建立繪圖的圖形物件

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

另請參閱