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();
}
}
1 Answer 1
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();
}
}
-
\$\begingroup\$ 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? \$\endgroup\$t3chb0t– t3chb0t2018年07月25日 19:34:51 +00:00Commented Jul 25, 2018 at 19:34
-
1\$\begingroup\$ 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 \$\endgroup\$Windex– Windex2018年07月25日 19:41:57 +00:00Commented Jul 25, 2018 at 19:41
ImageList
? Is this a win-forms control or something else that you've built? \$\endgroup\$