ImageList.ImageCollection Class

 

Encapsulates the collection of Image objects in an ImageList.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

System.Object
  System.Windows.Forms.ImageList.ImageCollection

Public NotInheritable Class ImageCollection
	Implements IList, ICollection, IEnumerable

NameDescription
System_CAPS_pubpropertyCount

Gets the number of images currently in the list.

System_CAPS_pubpropertyEmpty

Gets a value indicating whether the ImageList has any images.

System_CAPS_pubpropertyIsReadOnly

Gets a value indicating whether the list is read-only.

System_CAPS_pubpropertyItem(Int32)

Gets or sets an Image at the specified index within the collection.

System_CAPS_pubpropertyItem(String)

Gets an Image with the specified key from the collection.

System_CAPS_pubpropertyKeys

Gets the collection of keys associated with the images in the ImageList.ImageCollection.

NameDescription
System_CAPS_pubmethodAdd(Icon)

Adds the specified icon to the ImageList.

System_CAPS_pubmethodAdd(Image)

Adds the specified image to the ImageList.

System_CAPS_pubmethodAdd(Image, Color)

Adds the specified image to the ImageList, using the specified color to generate the mask.

System_CAPS_pubmethodAdd(String, Icon)

Adds an icon with the specified key to the end of the collection.

System_CAPS_pubmethodAdd(String, Image)

Adds an image with the specified key to the end of the collection.

System_CAPS_pubmethodAddRange(Image())

Adds an array of images to the collection.

System_CAPS_pubmethodAddStrip(Image)

Adds an image strip for the specified image to the ImageList.

System_CAPS_pubmethodClear()

Removes all the images and masks from the ImageList.

System_CAPS_pubmethodContains(Image)

Not supported. The IList.Contains method indicates whether a specified object is contained in the list.

System_CAPS_pubmethodContainsKey(String)

Determines if the collection contains an image with the specified key.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGetEnumerator()

Returns an enumerator that can be used to iterate through the item collection.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodIndexOf(Image)

Not supported. The IndexOf method returns the index of a specified object in the list.

System_CAPS_pubmethodIndexOfKey(String)

Determines the index of the first occurrence of an image with the specified key in the collection.

System_CAPS_pubmethodRemove(Image)

Not supported. The IList.Remove method removes a specified object from the list.

System_CAPS_pubmethodRemoveAt(Int32)

Removes an image from the list.

System_CAPS_pubmethodRemoveByKey(String)

Removes the image with the specified key from the collection.

System_CAPS_pubmethodSetKeyName(Int32, String)

Sets the key for an image in the collection.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodICollection.CopyTo(Array, Int32)

This API supports the product infrastructure and is not intended to be used directly from your code. Copies the items in this collection to a compatible one-dimensional array, starting at the specified index of the target array.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Add(Object)

This API supports the product infrastructure and is not intended to be used directly from your code. Adds the specified image to the ImageList.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Contains(Object)

This API supports the product infrastructure and is not intended to be used directly from your code. Implements the Contains method. Throws a NotSupportedException in all cases.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.IndexOf(Object)

This API supports the product infrastructure and is not intended to be used directly from your code. Implements the IndexOf method. Throws a NotSupportedException in all cases.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Insert(Int32, Object)

This API supports the product infrastructure and is not intended to be used directly from your code. Implements the Insert method. Throws a NotSupportedException in all cases.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIList.Remove(Object)

This API supports the product infrastructure and is not intended to be used directly from your code. Implements the Remove. Throws a NotSupportedException in all cases.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.IsSynchronized

This API supports the product infrastructure and is not intended to be used directly from your code. Gets a value indicating whether access to the collection is synchronized (thread safe).

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.SyncRoot

This API supports the product infrastructure and is not intended to be used directly from your code. Gets an object that can be used to synchronize access to the collection.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIList.IsFixedSize

This API supports the product infrastructure and is not intended to be used directly from your code. Gets a value indicating whether the ImageList.ImageCollection has a fixed size.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIList.Item(Int32)

This API supports the product infrastructure and is not intended to be used directly from your code. Gets or sets an image in an existing ImageList.ImageCollection.

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast(Of TResult)()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType(Of TResult)()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

This is used to manage the images in the ImageList programmatically, providing methods to add and remove image objects.

The following code example shows how to select, remove, and display images, from the Images property of the ImageList class, which is an ImageList.ImageCollection type.

Imports System
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 'NewNew

    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

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: