Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert SystemIcons into ImageList

I try to convert System.Drawing.SystemIcons to System.Windows.Forms.ImageList for using it in a ListView

I try the following code :

ImageList iconlist = new ImageList();
PropertyInfo[] properties = typeof(SystemIcons).GetProperties();
foreach (PropertyInfo property in properties)
{
   Debug.WriteLine(property.Name);
   iconlist.Images.Add(property.Name, property.GetValue(SystemIcons).ToBitmap());
}

But it’s return : ‘SystemIcons’ is a type, which is not valid in the given context

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

When you want to get the value of a static property, you have to pass null as the instance. Additionally, GetValue() returns an object, so you have to cast it:

iconlist.Images.Add(property.Name, (Icon)property.GetValue(null));

Thankfully, ImageCollection.Add() already has an overload that takes an icon, so no need for ToBitmap().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading