共用方式為


如何:使用 Windows Form ImageList 元件加入或移除影像

通常會在與控制項建立關聯之前,在 Windows Forms ImageList 元件中填入影像。 不過,您可以在將影像清單與控制項建立關聯之後,新增和移除影像。

備註

當您移除影像時,請確認任何相關聯控制項的 ImageIndex 屬性仍然有效。

以程式設計方式新增影像

  • 使用影像清單 Images 屬性的 Add 方法。

    在下列程式碼範例中,為影像位置設定的路徑是 [我的文件] 資料夾。 您可以假設大部分執行 Windows 作業系統的電腦都會包含這個資料夾,因此會使用這個位置。 選擇此位置也可讓擁有最低系統存取層級的使用者更安全地執行應用程式。 下列程式碼範例會要求您具備已新增 ImageList 控制項的表單。

    Public Sub LoadImage()
       Dim myImage As System.Drawing.Image = _
         Image.FromFile _
       (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\Image.gif")
       ImageList1.Images.Add(myImage)
    End Sub
    
    public void addImage()
    {
    // Be sure that you use an appropriate escape sequence (such as the
    // @) when specifying the location of the file.
       System.Drawing.Image myImage =
         Image.FromFile
       (System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.Personal)
       + @"\Image.gif");
       imageList1.Images.Add(myImage);
    }
    
    public:
       void addImage()
       {
       // Replace the bold image in the following sample
       // with your own icon.
       // Be sure that you use an appropriate escape sequence (such as
       // \\) when specifying the location of the file.
          System::Drawing::Image ^ myImage =
             Image::FromFile(String::Concat(
             System::Environment::GetFolderPath(
             System::Environment::SpecialFolder::Personal),
             "\\Image.gif"));
          imageList1->Images->Add(myImage);
       }
    

若要新增具有索引鍵值的影像。

  • 使用影像清單 Images 屬性的其中一種 Add 方法來取得索引鍵值。

    在下列程式碼範例中,為影像位置設定的路徑是 [我的文件] 資料夾。 您可以假設大部分執行 Windows 作業系統的電腦都會包含這個資料夾,因此會使用這個位置。 選擇此位置也可讓擁有最低系統存取層級的使用者更安全地執行應用程式。 下列程式碼範例會要求您具備已新增 ImageList 控制項的表單。

    Public Sub LoadImage()
       Dim myImage As System.Drawing.Image = _
         Image.FromFile _
       (System.Environment.GetFolderPath _
       (System.Environment.SpecialFolder.Personal) _
       & "\Image.gif")
       ImageList1.Images.Add("myPhoto", myImage)
    End Sub
    
public void addImage()
{
// Be sure that you use an appropriate escape sequence (such as the
// @) when specifying the location of the file.
   System.Drawing.Image myImage =
     Image.FromFile
   (System.Environment.GetFolderPath
   (System.Environment.SpecialFolder.Personal)
   + @"\Image.gif");
   imageList1.Images.Add("myPhoto", myImage);
}

以程式設計方式移除所有影像

  • 使用 Remove 方法來移除單一影像

    ,-或-

    使用 Clear 方法來清除影像清單中的所有影像。

    ' Removes the first image in the image list
    ImageList1.Images.Remove(myImage)
    ' Clears all images in the image list
    ImageList1.Images.Clear()
    
// Removes the first image in the image list.
imageList1.Images.Remove(myImage);
// Clears all images in the image list.
imageList1.Images.Clear();

依索引鍵移除影像

  • 使用 RemoveByKey 方法,依索引鍵移除單一影像。

    ' Removes the image named "myPhoto" from the list.
    ImageList1.Images.RemoveByKey("myPhoto")
    
// Removes the image named "myPhoto" from the list.
imageList1.Images.RemoveByKey("myPhoto");

另請參閱