ImageList Class
Provides methods to manage a collection of Image objects. This class cannot be inherited.
For a list of all members of this type, see ImageList Members.
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.ImageList
[Visual Basic] NotInheritable Public Class ImageList Inherits Component [C#] public sealed class ImageList : Component [C++] public __gc __sealed class ImageList : public Component [JScript] public class ImageList extends Component
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
ImageList is typically used by other controls, such as the ListView, TreeView, or ToolBar. You can add bitmaps, icons, or meta files 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.
Example
[Visual Basic, C#, C++] The following code example shows selecting images, removing images and displaying images.
[Visual Basic] Imports System Imports System.Drawing Imports System.ComponentModel Imports System.Windows.Forms Namespace myImageRotator Public Class Form1 Inherits System.Windows.Forms.Form Protected components As Container Protected listBox1 As ListBox Protected label2 As Label Protected label3 As Label Protected label5 As Label Protected pictureBox1 As PictureBox Protected button1 As Button Protected button2 As Button Protected button3 As Button Protected button4 As Button Protected panel1 As Panel Protected imageList1 As ImageList Protected myGraphics As Graphics Protected openFileDialog1 As OpenFileDialog Private currentImage As Integer = 0 Public Sub New() 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) End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private Sub InitializeComponent() ' Initializations for listBox1, label2, pictureBox1, ' button2, button3, panel1, openFileDialog1, button4, label1, ' button1, and imageList1. End Sub Protected Sub button1_Click(sender As Object, e As System.EventArgs) displayNextImage() End Sub Protected Sub button2_Click(sender As Object, e As System.EventArgs) imageList1.Images.RemoveAt(listBox1.SelectedIndex) listBox1.Items.Remove(listBox1.SelectedIndex) End Sub Protected Sub button3_Click(sender As Object, e As System.EventArgs) imageList1.Images.Clear() End Sub Protected Sub button4_Click(sender As Object, e As System.EventArgs) 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(imageToLoad As String) If imageToLoad <> "" Then imageList1.Images.Add(Image.FromFile(imageToLoad)) listBox1.BeginUpdate() listBox1.Items.Add(imageToLoad) listBox1.EndUpdate() End If End Sub Sub displayNextImage() If imageList1.Images.Empty <> True Then If imageList1.Images.Count - 1 < currentImage Then currentImage += 1 Else currentImage = 0 End If panel1.Refresh() imageList1.Draw(myGraphics, 10, 10, currentImage) pictureBox1.Image = imageList1.Images(currentImage) label3.Text = "Current image is " & currentImage listBox1.SelectedIndex = currentImage label5.Text = "Image is " & listBox1.Text End If End Sub Public Shared Sub Main() Application.Run(New Form1()) End Sub End Class End Namespace [C#] namespace myImageRotator { using System; using System.Drawing; using System.ComponentModel; using System.Windows.Forms; public class Form1 : 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.ImageSize = new Size(255,255); imageList1.TransparentColor = Color.White; // Assigns the graphics object to use in the draw options. myGraphics = Graphics.FromHwnd(panel1.Handle); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { // Initializations for listBox1, label2, pictureBox1, // button2, button3, panel1, openFileDialog1, button4, label1, // button1, and imageList1. } protected void button1_Click (object sender, System.EventArgs e) { displayNextImage(); } protected void button2_Click (object sender, System.EventArgs e) { imageList1.Images.RemoveAt(listBox1.SelectedIndex); listBox1.Items.Remove(listBox1.SelectedIndex); } protected void button3_Click (object sender, System.EventArgs e) { imageList1.Images.Clear(); } protected 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(); } } void displayNextImage() { if(imageList1.Images.Empty != true) { if(imageList1.Images.Count-1 < currentImage) { currentImage++; } else currentImage=0; panel1.Refresh(); imageList1.Draw(myGraphics,10,10,currentImage); pictureBox1.Image = imageList1.Images[currentImage]; label3.Text = "Current image is " + currentImage ; listBox1.SelectedIndex = currentImage; label5.Text = "Image is " + listBox1.Text ; } } public static void Main(string[] args) { Application.Run(new Form1()); } } } [C++] #using <mscorlib.dll> #using <System.Windows.Forms.dll> #using <System.dll> #using <System.Drawing.dll> using namespace System; using namespace System::Drawing; using namespace System::ComponentModel; using namespace System::Windows::Forms; public __gc class Form1 : public System::Windows::Forms::Form { protected: System::ComponentModel::Container* components; ListBox* listBox1; Label* label2; Label* label3; Label* label5; PictureBox* pictureBox1; Button* button1; Button* button2; Button* button3; Button* button4; Panel* panel1; ImageList* imageList1; Graphics* myGraphics; OpenFileDialog* openFileDialog1; private: int currentImage; public: Form1() { currentImage = 0; InitializeComponent(); imageList1 = new ImageList () ; // 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); } protected: void Dispose( bool disposing ) { if( disposing ) { if (components != 0) { components->Dispose(); } } Form::Dispose( disposing ); } private: void InitializeComponent() { // Initializations for listBox1, label2, pictureBox1, // button2, button3, panel1, openFileDialog1, button4, label1, // button1, and imageList1. } protected: void button1_Click (Object* /*sender*/, System::EventArgs* /*e*/) { displayNextImage(); } void button2_Click (Object* /*sender*/, System::EventArgs* /*e*/) { imageList1->Images->RemoveAt(listBox1->SelectedIndex); listBox1->Items->Remove(__box(listBox1->SelectedIndex)); } void button3_Click (Object* /*sender*/, System::EventArgs* /*e*/) { imageList1->Images->Clear(); } void button4_Click (Object* /*sender*/, System::EventArgs* /*e*/) { openFileDialog1->Multiselect = true ; if(openFileDialog1->ShowDialog() == DialogResult::OK) { if (openFileDialog1->FileNames != 0) { for(int i =0 ; i < openFileDialog1->FileNames->Length ; i++ ) { addImage(openFileDialog1->FileNames[i]); } } else addImage(openFileDialog1->FileName); } } private: void addImage(String* imageToLoad) { if (!imageToLoad->Equals(S"")) { imageList1->Images->Add(Image::FromFile(imageToLoad)); listBox1->BeginUpdate(); listBox1->Items->Add(imageToLoad); listBox1->EndUpdate(); } } void displayNextImage() { if(imageList1->Images->Empty != true) { if(imageList1->Images->Count-1 < currentImage) { currentImage++; } else currentImage=0; panel1->Refresh(); imageList1->Draw(myGraphics,10,10,currentImage); pictureBox1->Image = imageList1->Images->Item[currentImage]; label3->Text = String::Format( S"Current image is {0}", __box(currentImage)) ; listBox1->SelectedIndex = currentImage; label5->Text = String::Format( S"Image is {0}", listBox1->Text ) ; } } }; int main() { Application::Run(new Form1()); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Windows.Forms
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
See Also
ImageList Members | System.Windows.Forms Namespace | Bitmap | Icon | Metafile