Export (0) Print
Expand All

ImageList Class

Provides methods to manage a collection of Image objects. This class cannot be inherited.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

'Declaration
Public NotInheritable Class ImageList
	Inherits Component
'Usage
Dim instance As ImageList

public final class ImageList extends Component
public final class ImageList extends Component

ImageList is typically used by other controls, such as the ListView, TreeView, or ToolBar. You can add bitmaps or icons to the ImageList, and the other controls are able to use the images as they require.

ImageList uses a handle to manage the list of images. The Handle is not created until certain operations, including getting the Count, getting the Handle, and calling Draw, are performed on the image list.

The following code example shows how to select, remove, and display images.

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 'New

    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 Not (openFileDialog1.FileNames Is 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

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

package myImageRotator; 

import System.*;
import System.Drawing.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;

public class Form1 extends System.Windows.Forms.Form
{
    protected Container components;
    protected ListBox listBox1;
    protected Label label2;
    protected Label label3;
    protected Label label5;
    protected PictureBox pictureBox1;
    protected Button button1;
    protected Button button2;
    protected Button button3;
    protected Button button4;
    protected Panel panel1;
    protected ImageList imageList1;
    protected Graphics myGraphics;
    protected OpenFileDialog openFileDialog1;
    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.set_ImageSize(new Size(255, 255));
        imageList1.set_TransparentColor(Color.get_White());
        // Assigns the graphics object to use in the draw options.
        myGraphics = Graphics.FromHwnd(panel1.get_Handle());
    } //Form1

    protected void Dispose(boolean disposing)
    {
        if (disposing) {
            if (components != null) {
                components.Dispose();
            }
        }
        super.Dispose(disposing);
    } //Dispose

    private void InitializeComponent()
    {
        // Initializations for listBox1, label2, pictureBox1,
        // button2, button3, panel1, openFileDialog1, button4, label1, 
        // button1, and imageList1.
    } //InitializeComponent

    protected void button1_Click(Object sender, System.EventArgs e)
    {
        DisplayNextImage();
    } //button1_Click

    protected void button2_Click(Object sender, System.EventArgs e)
    {
        imageList1.get_Images().RemoveAt(listBox1.get_SelectedIndex());
        listBox1.get_Items().Remove(new Integer(listBox1.
            get_SelectedIndex()));
    } //button2_Click

    protected void button3_Click(Object sender, System.EventArgs e)
    {
        imageList1.get_Images().Clear();
    } //button3_Click

    protected void button4_Click(Object sender, System.EventArgs e)
    {
        openFileDialog1.set_Multiselect(true);
        if (openFileDialog1.ShowDialog().Equals(get_DialogResult().OK)) {
            if (openFileDialog1.get_FileNames() != null) {
                for (int i = 0; i < openFileDialog1.get_FileNames().length;
                    i++) {
                    AddImage(openFileDialog1.get_FileNames()[i]);
                }
            }
            else {
                AddImage(openFileDialog1.get_FileName());
            }
        }
    } //button4_Click

    private void AddImage(String imageToLoad)
    {
        if (!(imageToLoad.Equals(""))) {
            imageList1.get_Images().Add(Image.FromFile(imageToLoad));
            listBox1.BeginUpdate();
            listBox1.get_Items().Add(imageToLoad);
            listBox1.EndUpdate();
        }
    } //AddImage

    void DisplayNextImage()
    {
        if (imageList1.get_Images().get_Empty() != true) {
            if (imageList1.get_Images().get_Count() - 1 < currentImage) {
                currentImage++;
            }
            else {
                currentImage = 0;
            }
            panel1.Refresh();
            imageList1.Draw(myGraphics, 10, 10, currentImage);
            pictureBox1.set_Image(imageList1.get_Images().
                get_Item(currentImage));
            label3.set_Text("Current image is " + currentImage);
            listBox1.set_SelectedIndex(currentImage);
            label5.set_Text("Image is " + listBox1.get_Text());
        }
    } //DisplayNextImage

    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main
} //Form1

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
      System.Windows.Forms.ImageList

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft