다음을 통해 공유


방법: Windows Forms 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");

참고하십시오