# Loading icons in two different sizes

I am loading 3000 individual icons at run time one of my controls uses large icons, the other uses small icons. This works however seems terribly inefficient. The only way I can get the icons to be available in the small/large size is to have multiple image lists and have to load the file twice into each list.

 public partial class Form1 : Form
{

ImageList imageListSmall = new ImageList();
ImageList imageListLarge = new ImageList();
BackgroundWorker IconLoaderBGWorker = new BackgroundWorker();
DirectoryInfo IconDir = new DirectoryInfo("icons");

FileInfo[] IconFiles;

public Form1()
{

InitializeComponent();

// Setup Icon Lists
imageListSmall.ImageSize = new Size(16, 16);
imageListSmall.ColorDepth = ColorDepth.Depth32Bit;
imageListLarge.ImageSize = new Size(32, 32);
imageListLarge.ColorDepth = ColorDepth.Depth32Bit;

// Setup Progress bar events and Icon Loader
IconLoaderBGWorker.DoWork += new DoWorkEventHandler(bg_DoWork);
IconLoaderBGWorker.WorkerReportsProgress = true;
IconLoaderBGWorker.ProgressChanged += new ProgressChangedEventHandler(bg_ProgressChanged);
IconLoaderBGWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);

IconFiles = IconDir.GetFiles("*.png");
int allFiles = IconFiles.Count();
progressBar1.Maximum = allFiles;

}

private void bg_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}

private void bg_DoWork(object sender, DoWorkEventArgs e)
{
int i = 0;

foreach (FileInfo fileinfo in IconFiles)
{
imageListLarge.Images.Add(Path.GetFileNameWithoutExtension(fileinfo.Name), Image.FromFile(fileinfo.FullName));
imageListSmall.Images.Add(Path.GetFileNameWithoutExtension(fileinfo.Name), Image.FromFile(fileinfo.FullName));
IconLoaderBGWorker.ReportProgress(++i);
Application.DoEvents();
}

}

private void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
object result = e.Result;
progressBar1.Dispose();
}
}

• You're saying that the process is inefficient. How much time does it take to load the icons? Also plsease include the rest of the code. There are a couple of variables that are undefined in this context. – t3chb0t Jul 25 '18 at 14:54
• I added the additional code for missing variables. It takes about 30 seconds for the application to load. If i omit the small icons then it takes about 15 seconds to load which makes sense considering its adding the icons to one list instead of two. It isn't a horrific amount of time its just far more time than I would like. I was hoping there was some way to only load the images once, and have them able to be copied to the other list in a new size format. – Windex Jul 25 '18 at 15:52
• One more question: what is ImageList? Is this a win-forms control or something else that you've built? – t3chb0t Jul 25 '18 at 16:05
• Its part of System.Windows.Forms after its loaded it gets set to a listview image list so items in the control can be displayed as icons. – Windex Jul 25 '18 at 18:43

## 1 Answer

I guess I was able to figure it out, not sure how I missed it but by simply adjusting the bg_DoWork the application loads twice as fast, as it only creates an Image once instead of twice.

    private void bg_DoWork(object sender, DoWorkEventArgs e)
{
int i = 0;
Image tempImage;
foreach (FileInfo fileinfo in IconFiles)
{
tempImage = Image.FromFile(fileinfo.FullName);
imageListLarge.Images.Add(Path.GetFileNameWithoutExtension(fileinfo.Name), tempImage);
imageListSmall.Images.Add(Path.GetFileNameWithoutExtension(fileinfo.Name), tempImage);
IconLoaderBGWorker.ReportProgress(++i);
Application.DoEvents();
}
}

• This is still confusing. You are saying that you are loading images in two different sizes... but how, when each list uses the same image? Where is the small one and where is the larger one? – t3chb0t Jul 25 '18 at 19:34
• As shown in the constructor: // Setup Icon Lists imageListSmall.ImageSize = new Size(16, 16); imageListSmall.ColorDepth = ColorDepth.Depth32Bit; imageListLarge.ImageSize = new Size(32, 32); imageListLarge.ColorDepth = ColorDepth.Depth32Bit; The 'size' of the icon is dictated by the ImageSize property of the ImageList. The actual real size of the icon is irrelevant, its the same icon, one shrunk to 16x16 and one loaded in normal size of 32x32 – Windex Jul 25 '18 at 19:41