ImageList.ImageCollection Classe

Definizione

Incapsula la raccolta di oggetti Image inclusi in ImageList.

public: ref class ImageList::ImageCollection sealed : System::Collections::IList
public sealed class ImageList.ImageCollection : System.Collections.IList
type ImageList.ImageCollection = class
    interface IList
    interface ICollection
    interface IEnumerable
Public NotInheritable Class ImageList.ImageCollection
Implements IList
Ereditarietà
ImageList.ImageCollection
Implementazioni

Esempio

Nell'esempio di codice seguente viene illustrato come selezionare, rimuovere e visualizzare immagini dalla Images proprietà della ImageList classe , ovvero un ImageList.ImageCollection tipo.

namespace myImageRotator
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
{
    using namespace System;
    using namespace System::Drawing;
    using namespace System::ComponentModel;
    using namespace System::Windows::Forms;

    public ref class Form1 : public 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;

    public:
        Form1()
        {
            InitializeComponent();
            imageList1 = gcnew ImageList () ;
            currentImage = 0;

            // The default image size is 16 x 16, which sets up a larger
            // image size. 
            imageList1->ImageSize = System::Drawing::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 = gcnew System::ComponentModel::Container();
            this->listBox1 = gcnew System::Windows::Forms::ListBox();
            this->label3 = gcnew System::Windows::Forms::Label();
            this->button1 = gcnew System::Windows::Forms::Button();
            this->button2 = gcnew System::Windows::Forms::Button();
            this->button3 = gcnew System::Windows::Forms::Button();
            this->button4 = gcnew System::Windows::Forms::Button();
            this->pictureBox1 = gcnew System::Windows::Forms::PictureBox();
            this->imageList1 = gcnew System::Windows::Forms::ImageList(this->components);
            this->openFileDialog1 = gcnew System::Windows::Forms::OpenFileDialog();
            this->panel1 = gcnew System::Windows::Forms::Panel();
            this->label5 = gcnew System::Windows::Forms::Label();
            this->SuspendLayout();

            this->listBox1->Location = System::Drawing::Point(16, 16);
            this->listBox1->Size = System::Drawing::Size(400, 95);
            this->listBox1->TabIndex = 0;

            this->label3->Location = System::Drawing::Point(24, 168);
            this->label3->Text = "label3";

            this->button1->Location = System::Drawing::Point(96, 128);
            this->button1->Size = System::Drawing::Size(104, 23);
            this->button1->Text = "Show Next Image";
            this->button1->Click += gcnew System::EventHandler(this,&Form1::button1_Click);

            this->button2->Location = System::Drawing::Point(208, 128);
            this->button2->Size = System::Drawing::Size(104, 23);
            this->button2->Text = "Remove Image";
            this->button2->Click += gcnew System::EventHandler(this,&Form1::button2_Click);

            this->button3->Location = System::Drawing::Point(320, 128);
            this->button3->Text = "Clear List";
            this->button3->Click += gcnew System::EventHandler(this,&Form1::button3_Click);

            this->button4->Location = System::Drawing::Point(16, 128);
            this->button4->Text = "Open Image";
            this->button4->Click += gcnew System::EventHandler(this,&Form1::button4_Click);

            this->pictureBox1->Location = System::Drawing::Point(328, 232);
            this->pictureBox1->Size = System::Drawing::Size(336, 192);

            this->imageList1->ImageSize = System::Drawing::Size(16, 16);
            this->imageList1->TransparentColor = System::Drawing::Color::Transparent;

            this->panel1->Location = System::Drawing::Point(8, 240);
            this->panel1->Size = System::Drawing::Size(296, 184);

            this->label5->Location = System::Drawing::Point(168, 168);
            this->label5->Size = System::Drawing::Size(312, 40);
            this->label5->Text = "label5";

            this->ClientSize = 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 ;
                label5->Text = String::Concat("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() == System::Windows::Forms::DialogResult::OK)
            {
                if (openFileDialog1->FileNames != nullptr)
                {
                    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();
            }
        }

    public:
        static void Main(array<String^>^ /*args*/) 
        {
            Application::Run(gcnew Form1());
        }
    };

}
[STAThreadAttribute] 
int main(){
    myImageRotator::Form1::Main(nullptr);
}
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());
        }
    }
}
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private listBox1 As System.Windows.Forms.ListBox
    Private label3 As System.Windows.Forms.Label
    Private WithEvents button1 As System.Windows.Forms.Button
    Private WithEvents button2 As System.Windows.Forms.Button
    Private WithEvents button3 As System.Windows.Forms.Button
    Private WithEvents button4 As System.Windows.Forms.Button
    Private pictureBox1 As System.Windows.Forms.PictureBox
    Private imageList1 As System.Windows.Forms.ImageList
    Private openFileDialog1 As System.Windows.Forms.OpenFileDialog
    Protected myGraphics As Graphics
    Private panel1 As System.Windows.Forms.Panel
    Private label5 As System.Windows.Forms.Label
    Private currentImage As Integer = 0

    Public Sub New()
        imageList1 = New ImageList()

        InitializeComponent()
        ' 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)

    End Sub

    Private Sub InitializeComponent()

        Me.listBox1 = New System.Windows.Forms.ListBox()
        Me.label3 = New System.Windows.Forms.Label()
        Me.button1 = New System.Windows.Forms.Button()
        Me.button2 = New System.Windows.Forms.Button()
        Me.button3 = New System.Windows.Forms.Button()
        Me.button4 = New System.Windows.Forms.Button()
        Me.pictureBox1 = New System.Windows.Forms.PictureBox()
        Me.openFileDialog1 = New System.Windows.Forms.OpenFileDialog()
        Me.panel1 = New System.Windows.Forms.Panel()
        Me.label5 = New System.Windows.Forms.Label()
        Me.SuspendLayout()

        Me.listBox1.Location = New System.Drawing.Point(16, 16)
        Me.listBox1.Size = New System.Drawing.Size(400, 95)
        Me.listBox1.TabIndex = 0

        Me.label3.Location = New System.Drawing.Point(24, 168)
        Me.label3.Text = "label3"

        Me.button1.Location = New System.Drawing.Point(96, 128)
        Me.button1.Size = New System.Drawing.Size(104, 23)
        Me.button1.Text = "Show Next Image"

        Me.button2.Location = New System.Drawing.Point(208, 128)
        Me.button2.Size = New System.Drawing.Size(104, 23)
        Me.button2.Text = "Remove Image"

        Me.button3.Location = New System.Drawing.Point(320, 128)
        Me.button3.Text = "Clear List"

        Me.button4.Location = New System.Drawing.Point(16, 128)
        Me.button4.Text = "Open Image"

        Me.pictureBox1.Location = New System.Drawing.Point(328, 232)
        Me.pictureBox1.Size = New System.Drawing.Size(336, 192)

        Me.imageList1.ImageSize = New System.Drawing.Size(16, 16)
        Me.imageList1.TransparentColor = System.Drawing.Color.Transparent

        Me.panel1.Location = New System.Drawing.Point(8, 240)
        Me.panel1.Size = New System.Drawing.Size(296, 184)

        Me.label5.Location = New System.Drawing.Point(168, 168)
        Me.label5.Size = New System.Drawing.Size(312, 40)
        Me.label5.Text = "label5"

        Me.ClientSize = New System.Drawing.Size(672, 461)
        Me.Controls.Add(label5)
        Me.Controls.Add(panel1)
        Me.Controls.Add(pictureBox1)
        Me.Controls.Add(button4)
        Me.Controls.Add(button3)
        Me.Controls.Add(button2)
        Me.Controls.Add(button1)
        Me.Controls.Add(label3)
        Me.Controls.Add(listBox1)
        Me.ResumeLayout(False)
    End Sub

    ' Display the image.
    Private Sub button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles button1.Click

        If imageList1.Images.Empty <> True Then
            If imageList1.Images.Count - 1 > currentImage Then
                currentImage += 1
            Else
                currentImage = 0
            End If
            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.ToString
            listBox1.SelectedIndex = currentImage
            label5.Text = "Image is " + listBox1.Text
        End If
    End Sub

    ' Remove the image.
    Private Sub button2_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles button2.Click

        imageList1.Images.RemoveAt(listBox1.SelectedIndex)
        listBox1.Items.Remove(listBox1.SelectedItem)
    End Sub

    ' Clear all images.
    Private Sub button3_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles button3.Click
        imageList1.Images.Clear()
        listBox1.Items.Clear()
    End Sub

    ' Find an image.
    Private Sub button4_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles button4.Click

        openFileDialog1.Multiselect = True
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            If (openFileDialog1.FileNames IsNot Nothing) Then
                Dim i As Integer
                For i = 0 To openFileDialog1.FileNames.Length - 1
                    addImage(openFileDialog1.FileNames(i))
                Next i
            Else
                addImage(openFileDialog1.FileName)
            End If
        End If
    End Sub

    Private Sub addImage(ByVal imageToLoad As String)
        If imageToLoad <> "" Then
            imageList1.Images.Add(Image.FromFile(imageToLoad))
            listBox1.BeginUpdate()
            listBox1.Items.Add(imageToLoad)
            listBox1.EndUpdate()
        End If
    End Sub

    <StaThread()> _
    Public Shared Sub Main(ByVal args() As String)
        Application.Run(New Form1())
    End Sub
End Class

Commenti

Viene usato per gestire le immagini nell'oggetto ImageList a livello di codice, fornendo metodi per aggiungere e rimuovere oggetti immagine.

Proprietà

Count

Ottiene il numero delle immagini attualmente nell'elenco.

Empty

Ottiene un valore che indica se l'oggetto ImageList contiene immagini.

IsReadOnly

Ottiene un valore che indica se l'elenco è di sola lettura.

Item[Int32]

Ottiene o imposta un oggetto Image in corrispondenza dell'indice specificato all'interno dell'insieme.

Item[String]

Ottiene dall'insieme un oggetto Image con la chiave specificata.

Keys

Ottiene l'insieme delle chiavi associate alle immagini nell'insieme ImageList.ImageCollection.

Metodi

Add(Icon)

Aggiunge l'icona specificata all'oggetto ImageList.

Add(Image)

Aggiunge l'immagine specificata all'oggetto ImageList.

Add(Image, Color)

Aggiunge l'immagine specificata all'oggetto ImageList, utilizzando il colore specificato per generare la maschera.

Add(String, Icon)

Aggiunge un'icona con la chiave specificata alla fine dell'insieme.

Add(String, Image)

Aggiunge un'immagine con la chiave specificata alla fine dell'insieme.

AddRange(Image[])

Aggiunge una matrice di immagini all'insieme.

AddStrip(Image)

Aggiunge all'oggetto ImageList un elenco immagini per l'immagine specificata.

Clear()

Rimuove tutte le immagini e le maschere dall'oggetto ImageList.

Contains(Image)

Non supportata. Il metodo Contains(Object) indica se un oggetto specificato è contenuto nell'elenco.

ContainsKey(String)

Determina se l'insieme contiene un'immagine con la chiave specificata.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetEnumerator()

Restituisce un enumeratore che può essere utilizzato per scorrere l'insieme di elementi.

GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
IndexOf(Image)

Non supportata. Il metodo IndexOf(Object) restituisce l'indice di un oggetto specificato nell'insieme.

IndexOfKey(String)

Determina l'indice della prima occorrenza di un'immagine con la chiave specificata nell'insieme.

MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
Remove(Image)

Non supportata. Il metodo Remove(Object) rimuove un oggetto specificato dall'insieme.

RemoveAt(Int32)

Rimuove un'immagine dall’elenco.

RemoveByKey(String)

Rimuove l'immagine con la chiave specificata dall'insieme.

SetKeyName(Int32, String)

Imposta la chiave di un'immagine nell'insieme.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

ICollection.CopyTo(Array, Int32)

Copia gli elementi di questo insieme in una matrice compatibile unidimensionale, a partire dall'indice specificato della matrice di destinazione.

ICollection.IsSynchronized

Ottiene un valore che indica se l'accesso alla raccolta è sincronizzato (thread-safe).

ICollection.SyncRoot

Ottiene un oggetto che può essere usato per sincronizzare l'accesso alla raccolta.

IList.Add(Object)

Aggiunge l'immagine specificata all'oggetto ImageList.

IList.Contains(Object)

Questa API supporta l'infrastruttura del prodotto e non è previsto che venga usata direttamente dal codice.

Implementa il metodo Contains(Object). Genera un'eccezione NotSupportedException in tutti i casi.

IList.IndexOf(Object)

Questa API supporta l'infrastruttura del prodotto e non è previsto che venga usata direttamente dal codice.

Implementa il metodo IndexOf(Object). Genera un'eccezione NotSupportedException in tutti i casi.

IList.Insert(Int32, Object)

Implementa il metodo Insert(Int32, Object). Genera un'eccezione NotSupportedException in tutti i casi.

IList.IsFixedSize

Ottiene un valore che indica se ImageList.ImageCollection ha dimensioni fisse.

IList.Item[Int32]

Ottiene o imposta un'immagine in un insieme ImageList.ImageCollection esistente.

IList.Remove(Object)

Implementa il metodo Remove(Object). Genera un'eccezione NotSupportedException in tutti i casi.

Metodi di estensione

Cast<TResult>(IEnumerable)

Esegue il cast degli elementi di un oggetto IEnumerable nel tipo specificato.

OfType<TResult>(IEnumerable)

Filtra gli elementi di un oggetto IEnumerable in base a un tipo specificato.

AsParallel(IEnumerable)

Consente la parallelizzazione di una query.

AsQueryable(IEnumerable)

Converte un oggetto IEnumerable in un oggetto IQueryable.

Si applica a