Leer en inglés

Compartir a través de


ImageList.ImageCollection Clase

Definición

Encapsula la colección de objetos Image en una clase ImageList.

C#
public sealed class ImageList.ImageCollection : System.Collections.IList
Herencia
ImageList.ImageCollection
Implementaciones

Ejemplos

En el ejemplo de código siguiente se muestra cómo seleccionar, quitar y mostrar imágenes de la Images propiedad de la ImageList clase , que es un ImageList.ImageCollection tipo.

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());
        }
    }
}

Comentarios

Esto se usa para administrar las imágenes en mediante ImageList programación, proporcionando métodos para agregar y quitar objetos de imagen.

Propiedades

Count

Obtiene el número de imágenes incluidas actualmente en la lista.

Empty

Obtiene un valor que indica si ImageList tiene alguna imagen.

IsReadOnly

Obtiene un valor que indica si la lista es de solo lectura.

Item[Int32]

Obtiene o establece un control Image en el índice especificado de la colección.

Item[String]

Obtiene un control Image con la clave especificada de la colección.

Keys

Obtiene la colección de claves asociada a las imágenes en el control ImageList.ImageCollection.

Métodos

Add(Icon)

Agrega el icono especificado a ImageList.

Add(Image)

Agrega la imagen especificada a ImageList.

Add(Image, Color)

Agrega la imagen especificada a ImageList, mediante el uso del color especificado para generar la máscara.

Add(String, Icon)

Agrega un icono con la clave especificada al final de la colección.

Add(String, Image)

Agrega una imagen con la clave especificada al final de la colección.

AddRange(Image[])

Agrega una matriz de imágenes a la colección.

AddStrip(Image)

Agrega una banda de imagen a la imagen especificada del control ImageList.

Clear()

Quita todas las imágenes y las máscaras de ImageList.

Contains(Image)

No se admite. El método Contains(Object) indica si el objeto especificada está en la lista.

ContainsKey(String)

Determina si la colección contiene una imagen con la clave especificada.

Equals(Object)

Determina si el objeto especificado es igual que el objeto actual.

(Heredado de Object)
GetEnumerator()

Devuelve un enumerador que puede utilizarse para recorrer una iteración en la colección de elementos.

GetHashCode()

Sirve como la función hash predeterminada.

(Heredado de Object)
GetType()

Obtiene el Type de la instancia actual.

(Heredado de Object)
IndexOf(Image)

No se admite. El método IndexOf(Object) devuelve el índice de un objeto especificado en la lista.

IndexOfKey(String)

Determina el índice de la primera aparición de una imagen con la clave especificada en la colección.

MemberwiseClone()

Crea una copia superficial del Object actual.

(Heredado de Object)
Remove(Image)

No se admite. El método Remove(Object) quita un objeto especificado de la lista.

RemoveAt(Int32)

Quita una imagen de la lista.

RemoveByKey(String)

Quita la imagen con la clave especificada de la colección.

SetKeyName(Int32, String)

Establece la clave para una imagen en la colección.

ToString()

Devuelve una cadena que representa el objeto actual.

(Heredado de Object)

Implementaciones de interfaz explícitas

ICollection.CopyTo(Array, Int32)

Copia los elementos de esta colección en una matriz unidimensional compatible, empezando en el índice especificado de la matriz de destino.

ICollection.IsSynchronized

Obtiene un valor que indica si el acceso a la colección está sincronizado (es seguro para la ejecución de subprocesos).

ICollection.SyncRoot

Obtiene un objeto que se puede utilizar para sincronizar el acceso a la colección.

IList.Add(Object)

Agrega la imagen especificada a ImageList.

IList.Contains(Object)

Esta API admite la infraestructura de producto y no está pensada para usarse directamente en el código.

Implementa el método Contains(Object). Devuelve NotSupportedException en todos los casos

IList.IndexOf(Object)

Esta API admite la infraestructura de producto y no está pensada para usarse directamente en el código.

Implementa el método IndexOf(Object). Devuelve NotSupportedException en todos los casos

IList.Insert(Int32, Object)

Implementa el método Insert(Int32, Object). Devuelve NotSupportedException en todos los casos

IList.IsFixedSize

Obtiene un valor que indica si la interfaz ImageList.ImageCollection tiene un tamaño fijo.

IList.Item[Int32]

Obtiene o establece una imagen en un objeto ImageList.ImageCollection existente.

IList.Remove(Object)

Implementa Remove(Object). Devuelve NotSupportedException en todos los casos

Métodos de extensión

Cast<TResult>(IEnumerable)

Convierte los elementos de IEnumerable en el tipo especificado.

OfType<TResult>(IEnumerable)

Filtra los elementos de IEnumerable en función de un tipo especificado.

AsParallel(IEnumerable)

Habilita la paralelización de una consulta.

AsQueryable(IEnumerable)

Convierte una interfaz IEnumerable en IQueryable.

Se aplica a

Producto Versiones
.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