SIGN UP MEMBER LOGIN:    
ARTICLE

ImageList in C#

Posted by Mahesh Chand Articles | Windows Controls C# July 18, 2010
In this article, I will discuss how to create an ImageList control and how to use its properties and methods to use in a Windows Forms application.
Reader Level:
Download Files:
 


An ImageList is a supporting control that is typically used by other controls, such as a ListView but is exposed as a component to developers. We can use this component in our applications when we are building our own controls such as a photo gallery or an image rotator control. 
In this article, I will discuss how to create an ImageList control and how to use its properties and methods to use in a Windows Forms application.

Creating an ImageList

ImageList class represents the ImageList First step to create a dynamic ImageList is to create an instance of ImageList class. The following code snippet creates an ImageList control object.

ImageList photoList = new ImageList();

 

In the next step, you may set properties of an ImageList control. The following code snippet sets a few properties of an ImageList.

photoList.TransparentColor = Color.Blue;

photoList.ColorDepth = ColorDepth.Depth32Bit;

photoList.ImageSize = new Size(200, 200);

 

Unlike other Windows Forms control, you can't add ImageList control to a Form. You need to draw an ImageList control using the Draw method. The Draw method takes a Graphics object that is the handle of the container control that will be used as a drawing canvas.

 

photoList.Draw(g, new Point(20, 20), count);


Setting ImageList Properties

ColorDepth property represents the color depth of the image list.

ImageSize property represents the size of the images in the image list.

Images property represents all images in an ImageList as an ImageCollection object.

The following code snippet sets these properties and adds three images to the ImageList control and later loops through the images and displays them on a Form.

Graphics g = Graphics.FromHwnd(this.Handle);

 

ImageList photoList = new ImageList();

photoList.TransparentColor = Color.Blue;

photoList.ColorDepth = ColorDepth.Depth32Bit;

photoList.ImageSize = new Size(200, 200);

 

photoList.Images.Add(Image.FromFile(@"C:\Images\Garden.jpg"));

photoList.Images.Add(Image.FromFile(@"C:\Images\Tree.jpg"));

photoList.Images.Add(Image.FromFile(@"C:\Images\Waterfall.jpg"));

 

for (int count = 0; count < photoList.Images.Count; count++)

{

    photoList.Draw(g, new Point(20, 20), count);

 

    // Paint the form and wait to load the image

    Application.DoEvents();

    System.Threading.Thread.Sleep(1000);

}

 

Summary
In this article, we discussed discuss how to create and use an ImageList control in a Windows Forms application.

share this article :
post comment
 

Iis server making debug problm.so kindly solutn on rehanpq@gmail.com

Posted by Rehan Qureshi Jan 30, 2012

Hi Mahesh It always cool to view your code as it helped me a lot and even at times its the same what i was looking for. Would be glad to make you a friend and hope you would like it too. Looking forward for your reply. Regards

Posted by Jinal Shah Dec 05, 2011

Venkat,
Go to Windows Controls section of this site (home page >> Left side bar >> Click Windows Controls) or go to my profile page. I have written articles on most of the controls that includes both design-time and run-time creation of controls and set their properties and call their methods.

Cheers!

Posted by Mahesh Chand Aug 15, 2010

Did you download the attached project? Download it, change your image path and run it. It is a VS 2010 project.

For testing, you can drag and drop and ImageList onto a Form and add images to it at design time and check the path code code behind.

Posted by Mahesh Chand Aug 15, 2010

I do as u say. But the images are not shown in Form. Image Path is okay. I want to know why? Please reply me. Thank a lot.

Posted by Ei Cho Zin Aug 07, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor