ImageList控件(存储图像控件)用于存储图像资源,然后在控件上显示出来,这样就简化了对图像的管理。ImageList 控件的主要属性是 Images,它包含关联控件将要使用的图片。每个单独的图像可通过其索引值或其键值来访问。所有图像都将以同样的大小显示,该大小由ImageSize属性设置,较大的图像将缩小至适当的尺寸,如图 1 所示为 ImageList 控件。


C# ImageList 控件_显示图片


图1   ImageList控件

      ImageList 控件实际上就相当于一个图片集,也就是将多个图片存储到图片集中,当想要对某一图片进行操作时,只需根据其图片的编号,就可以找出该图片,并对其进行操作。




01


在 lmageList 控件中添加图像


      使用 ImageList 控件的 Images 属性的Add方法,可以以编程的方式向 lmageList 控件中添加图像,下面对 Add 方法进行详细介绍。

       Add 方法的功能是将指定图像添加到 ImageList中。

语法如下:

public void Add(lmage value)

value:要添加到列表中的图像。

【例 2】 创建一个 Windows 应用程序,首先获取图像的路径,然后通过ImageList控件的Images属性的 Add 方法向控件中添加图像。

代码如下:


    private void Form1_Load(object sender,EventArgs e)
    {
    //设置要加载的第一张图片的路径
    string Path = Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.
    StartupPath. LastIndexOf("\\")).LastIndexOf("\\"));
    Path += @"\01.jpg";
    //设置要加载的第二张图片的路径
    string Path2 = Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.
    StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
    Path2 += @"\02.jpg";
    Image Mimg=Image.FromFile(Path,true); //创建一个 Image 对象
    imageList1.Images.Add(Mimg); //使用 Images 属性的 Add 方法向控件中添加图像
    Image Mimg2=Image.FromFile(Path2,true); //创建一个 Image 对象
    imageList1.Images.Add(Mimg2); //使用 Images 属性的 Add 方法向控件中添加图像
    imageList1.ImageSize = new Size(200,165); //设置显示图片的大小
    pictureBox1.Width = 200; //设置 pictureBox1 控件的宽
    pictureBox1.Height=165; //设置 pictureBox1控件的高
    }
    private void button1_Click(object sender,EventArgs e)
    {
    //设置pictureBox1 的图像索引是imageList1、控件索引为0的图片
    pictureBox1.Image = imageList1.Imagesl[0];
    }
    private void button2_Click(object sender,EventArgs e)
    {
    //设置 pictureBox1 的图像索引是imageList1、控件索引为1的图片
    pictureBox1.Image=imageList1.Images[1];
    }


    程序的运行结果如图 2 所示.


    C# ImageList 控件_属性设置_02


    图2   显示添加后的图像


    说明 在向 ImageList 组件中存储图片时,可以通过该组件的 ImageSize 属性设置图片的尺寸,其默认尺寸是16x16,最大尺寸是256X256。





    02


    在 ImageList 控件中移除图像 


           在 ImageList 控件中可以使用 RemoveAt 方法移除单个图像或使用 Clear 方法清除图像列表中的所有图像。下面详细介绍 RemoveAt方法和 Clear 方法。

           RemoveAt 方法用于从列表中移除图像。

    语法如下:


    public void RemoveAt(int index)

    index: 要移除的图像的索引。 


    说明 在用 RemoveAt方法删除图片时,如果索引无效(超出范围),则发生运行异常。

    Clear方法主要用于从 ImageList 中移除所有图像。

    语法如下:

    public void Clear()

    【例 3】创建一个 Windows 应用程序,设置在控件上显示的图像,使用 Images 属性的 Add方法将其添加到控件中。然后运行程序,单击“加载图像”按钮显示图像,再单击“移除图像”按钮移除图像之后,重新单击“加载图像”按钮,将弹出“没有图像”的提示。

    代码如下:


      private void Form1_Load(object sender,EventArgs e)
      {
      pictureBox1.Width=200; //设置 pictureBox1控件的宽
      pictureBox1.Height=165; //设置 pictureBox1控件的高
      //设置要加载图片的路径
      string Path = Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.
      StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
      Path +=@"\01.jpg";
      Image img = Image.FromFile(Path,true); //创建 Image 对象
      imageList1.Images.Add(img); //使用 Images 属性的 Add 方法向控件中添加图像
      imageList1.ImageSize = new Size(200,165); //设置显示图片的大小
      }
      private void button1_Click(object sender,EventArgs e)
      {
      if(ImageList1.Images.Count ==0) //判断imageList1 中是否存在图像
      {
      MessageBox.Show("没有图像"); //如果没有图像弹出提示
      }
      else //否则
      {
      //使 pictureBox1控件显示imageList1 控件中索引为0的图像
      pictureBox1.Image = imageList1.Images[0];
      }
      }
      private void button2_Click(object sender, EventArgs e)
      {
      imageList1.Images.RemoveAt(0); //使用 RemoveAt方法移除图像
      }


      程序的运行结果如图 3 所示。


      C# ImageList 控件_属性设置_03


      图3   移除控件中的图像


      还可以使用Clear方法从ImageList中移除所有图像。

      代码如下:


      imageList1.Images.Clear();             //使用Clear方法移除所有图像