ImageList.ImageCollection 类

定义

封装 ImageListImage 对象的集合。

C#
public sealed class ImageList.ImageCollection : System.Collections.IList
继承
ImageList.ImageCollection
实现

示例

下面的代码示例演示如何从 Images 类的 ImageList 属性(类型 ImageList.ImageCollection )中选择、删除和显示图像。

C#
namespace myImageRotator
{
    using System;
    using System.Drawing;
    using System.ComponentModel;
    using System.Windows.Forms;
 
    public class Form1 : System.Windows.Forms.Form
    {
        private System.ComponentModel.IContainer components;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.ImageList imageList1;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        protected Graphics myGraphics;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label5;
        private int currentImage = 0;
 
        public Form1()
        {
            InitializeComponent();
            imageList1 = new ImageList () ;

            // The default image size is 16 x 16, which sets up a larger
            // image size. 
            imageList1.ImageSize = new Size(255,255);
            imageList1.TransparentColor = Color.White;

            // Assigns the graphics object to use in the draw options.
            myGraphics = Graphics.FromHwnd(panel1.Handle);
        }
 
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.label3 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label5 = new System.Windows.Forms.Label();
            this.SuspendLayout();

            this.listBox1.Location = new System.Drawing.Point(16, 16);
            this.listBox1.Size = new System.Drawing.Size(400, 95);
            this.listBox1.TabIndex = 0;

            this.label3.Location = new System.Drawing.Point(24, 168);
            this.label3.Text = "label3";

            this.button1.Location = new System.Drawing.Point(96, 128);
            this.button1.Size = new System.Drawing.Size(104, 23);
            this.button1.Text = "Show Next Image";
            this.button1.Click += new System.EventHandler(this.button1_Click);

            this.button2.Location = new System.Drawing.Point(208, 128);
            this.button2.Size = new System.Drawing.Size(104, 23);
            this.button2.Text = "Remove Image";
            this.button2.Click += new System.EventHandler(this.button2_Click);

            this.button3.Location = new System.Drawing.Point(320, 128);
            this.button3.Text = "Clear List";
            this.button3.Click += new System.EventHandler(this.button3_Click);

            this.button4.Location = new System.Drawing.Point(16, 128);
            this.button4.Text = "Open Image";
            this.button4.Click += new System.EventHandler(this.button4_Click);

            this.pictureBox1.Location = new System.Drawing.Point(328, 232);
            this.pictureBox1.Size = new System.Drawing.Size(336, 192);

            this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;

            this.panel1.Location = new System.Drawing.Point(8, 240);
            this.panel1.Size = new System.Drawing.Size(296, 184);

            this.label5.Location = new System.Drawing.Point(168, 168);
            this.label5.Size = new System.Drawing.Size(312, 40);
            this.label5.Text = "label5";

            this.ClientSize = new System.Drawing.Size(672, 461);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.listBox1);
            this.ResumeLayout(false);
        }

        // Display the image.
        private void button1_Click (object sender, System.EventArgs e)
        {
            if(imageList1.Images.Empty != true)
            {
                if(imageList1.Images.Count-1 > currentImage)
                {
                    currentImage++;
                }
                else
                {
                    currentImage=0;
                }
                panel1.Refresh();
                
                // Draw the image in the panel.
                imageList1.Draw(myGraphics,10,10,currentImage);

                // Show the image in the PictureBox.
                pictureBox1.Image = imageList1.Images[currentImage];
                label3.Text = "Current image is " + currentImage ;
                listBox1.SelectedIndex = currentImage;
                label5.Text = "Image is " + listBox1.Text ;
            }
        }
 
        // Remove the image.
        private void button2_Click (object sender, System.EventArgs e)
        {
            imageList1.Images.RemoveAt(listBox1.SelectedIndex);
            listBox1.Items.Remove(listBox1.SelectedItem);
        }
 
        // Clear all images.
        private void button3_Click (object sender, System.EventArgs e)
        {
            imageList1.Images.Clear();
            listBox1.Items.Clear();
        }
 
        // Find an image.
        private void button4_Click (object sender, System.EventArgs e)
        {
            openFileDialog1.Multiselect = true ;
            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog1.FileNames != null)
                {
                    for(int i =0 ; i < openFileDialog1.FileNames.Length ; i++ )
                    {
                        addImage(openFileDialog1.FileNames[i]);
                    }
                }
                else
                {
                    addImage(openFileDialog1.FileName);
                }
            }
        }
 
        private void addImage(string imageToLoad)
        {
            if (imageToLoad != "")
            {
                imageList1.Images.Add(Image.FromFile(imageToLoad));
                listBox1.BeginUpdate();
                listBox1.Items.Add(imageToLoad);
                listBox1.EndUpdate();
            }
        }
        [STAThread]
        public static void Main(string[] args) 
        {
            Application.Run(new Form1());
        }
    }
}

注解

这用于以编程方式管理 中的 ImageList 图像,并提供添加和删除图像对象的方法。

属性

Count

获取列表中的当前图像数。

Empty

获取一个值,通过该值指示 ImageList 是否包含任何图像。

IsReadOnly

获取一个值,该值指示列表是否为只读。

Item[Int32]

获取或设置集合中指定索引处的 Image

Item[String]

从集合中获取具有指定键的 Image

Keys

获取与 ImageList.ImageCollection 中的图像关联的键的集合。

方法

Add(Icon)

将指定图标添加到 ImageList 中。

Add(Image)

将指定图像添加到 ImageList 中。

Add(Image, Color)

将指定图像添加到 ImageList 中,使用指定颜色生成屏蔽。

Add(String, Icon)

将具有指定键的图标添加到集合的末尾处。

Add(String, Image)

将具有指定键的图像添加到集合的末尾处。

AddRange(Image[])

向集合中添加图像的数组。

AddStrip(Image)

将指定图像的图像条添加到 ImageList

Clear()

ImageList 中移除所有图像和屏蔽。

Contains(Image)

不支持。 Contains(Object) 方法指示列表中是否包含指定的对象。

ContainsKey(String)

确定集合是否包含具有指定键的图像。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetEnumerator()

返回一个可用于循环访问项集合的枚举数。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
IndexOf(Image)

不支持。 IndexOf(Object) 方法返回指定对象在列表中的索引。

IndexOfKey(String)

确定集合中具有指定键的首个图像的索引。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Remove(Image)

不支持。 Remove(Object) 方法从列表中移除指定的对象。

RemoveAt(Int32)

从列表中移除图像。

RemoveByKey(String)

从集合中移除具有指定键的图像。

SetKeyName(Int32, String)

为集合中的图像设置键。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

ICollection.CopyTo(Array, Int32)

从目标数组的指定索引处开始,将此集合中的项复制到一个兼容的一维数组。

ICollection.IsSynchronized

获取一个值,该值指示对集合的访问是否为同步的(线程安全)。

ICollection.SyncRoot

获取可用于同步对集合的访问的对象。

IList.Add(Object)

将指定图像添加到 ImageList 中。

IList.Contains(Object)

此 API 支持产品基础结构,不能在代码中直接使用。

实现 Contains(Object) 方法。 在所有情况下均引发 NotSupportedException

IList.IndexOf(Object)

此 API 支持产品基础结构,不能在代码中直接使用。

实现 IndexOf(Object) 方法。 在所有情况下均引发 NotSupportedException

IList.Insert(Int32, Object)

实现 Insert(Int32, Object) 方法。 在所有情况下均引发 NotSupportedException

IList.IsFixedSize

获取一个值,该值指示 ImageList.ImageCollection 是否具有固定大小。

IList.Item[Int32]

获取或设置现有 ImageList.ImageCollection 中的图像。

IList.Remove(Object)

实现 Remove(Object) 任务。 在所有情况下均引发 NotSupportedException

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。

AsParallel(IEnumerable)

启用查询的并行化。

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryable

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9