openFileDialog1.Multiselect = true;
imageList1.ImageSize = new Size(100, 75);
listView1.View = View.LargeIcon;
listView1.Font = new Font("arail", 8);
ListViewItem item;
//listView1.Columns.Add("0","Nothing",80,HorizontalAlignment.Center,0);
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
btnClearlist.Enabled = false;
lblDone.Text = "";
lblDragPhotos.Visible = false;
fileNames = openFileDialog1.FileNames;
imgList.ImageSize = new Size(100, 75);
imgList.ColorDepth = ColorDepth.Depth24Bit;
//Bitmap bmp = new Bitmap(95, 75);
//Graphics g = Graphics.FromImage(bmp);
//Pen p = new Pen(Color.Gray);
//g.FillRectangle(new SolidBrush(Color.LightGray), 0, 0, 100, 70);
//g.DrawRectangle(p, 1, 1, 93, 73);
//g.Dispose();
foreach (string s in fileNames)
{
try
{
if (imageList1.Images[s] is Image)
{ }
else
{
//imgList.Images.Add(s,bmp);
listView1.LargeImageList = imgList;
item = new ListViewItem();
string str=System.IO.Path.GetFileName(s);
if (str.Length > 18)
item.Text = str.Substring(0, 15)+"...";
else
item.Text = str;
item.ImageKey = s;
listView1.Items.Add(item);
}
}
catch
{
}
}
lblPhotos.Text = "Total photos are " + listView1.Items.Count;
foreach (string s in fileNames)
{
try
{
if (imageList1.Images[s] is Image)
{ }
else
{
Application.DoEvents();
Cursor.Current = Cursors.WaitCursor;
listView1.LargeImageList = imageList1;
//imgList.Images[imgList.Images.IndexOfKey(s)] = imageList1.Images[imageList1.Images.IndexOfKey(s)];
imageList1.Images.Add(s, Image.FromFile(s));
}
}
catch
{
}
}
listView1.LargeImageList = imageList1;
lblPhotos.Text = "Total photos are " + listView1.Items.Count;
if (listView1.Items.Count != 0)
{
lblDone.Text = "You can create now";
lblDone.Left = Convert.ToInt32((grpCollage.Width - lblDone.Width - 4) / 2);
btnClearlist.Enabled = true;
lblDragPhotos.Visible = false;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private class Thumbnail
{
public string fileName; // this is a full path filename!
public Bitmap bmp;
}
private Dictionary<string, ListViewItem> ListViewItems = new Dictionary<string, ListViewItem>();
private Size ImageSize = new Size(100, 75);
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
button1.Enabled = false;
imageList1.Images.Clear();
imageList1.ImageSize = this.ImageSize;
imageList1.ColorDepth = ColorDepth.Depth24Bit;
listView1.View = View.LargeIcon;
listView1.Font = new Font("Arial", 8);
listView1.LargeImageList = imageList1;
List<string> fileNames = new List<string>();
fileNames.AddRange(openFileDialog1.FileNames);
ListViewItems.Clear();
// load up BLANK ListViewItems for each image
listView1.Items.Clear();
listView1.BeginUpdate();
foreach (string fileName in fileNames)
{
ListViewItem lvi = listView1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(fileName));
lvi.Tag = fileName;
ListViewItems.Add(fileName, lvi);
}
listView1.EndUpdate();
// generate the thumbnails in the background thread
BackgroundWorker bgw = new BackgroundWorker();
bgw.WorkerReportsProgress = true;
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.RunWorkerAsync(fileNames);
}
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgw = (BackgroundWorker)sender;
Bitmap bmp;
PictureBox pb = new PictureBox();
pb.SizeMode = PictureBoxSizeMode.Zoom;
pb.Size = this.ImageSize;
List<string> fileNames = (List<string>)e.Argument;
foreach (string fileName in fileNames)
{
try
{
// create a thumbnail using an invisible picturebox
pb.Image = Image.FromFile(fileName);
bmp = new Bitmap(this.ImageSize.Width, this.ImageSize.Height);
pb.DrawToBitmap(bmp, pb.ClientRectangle);
// pass the thumbnail with its name out to the main UI
Thumbnail tb = new Thumbnail();
tb.fileName = fileName;
tb.bmp = bmp;
bgw.ReportProgress(-1, tb);
pb.Image = null;
// throttle the image loading background thread so it doesn't overload the main UI
// YES...this was necessary! Otherwise the main UI eventually got bogged down tyring
// to update with the new thumbnails being generated...
System.Threading.Thread.Sleep(50);
}
catch (Exception ex)
{
MessageBox.Show("FileName: " + fileName + "\r\n\r\n" + ex.ToString(), "Error Loading Image", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// add the new thumbnail to the Listview
Thumbnail tb = (Thumbnail)e.UserState;
imageList1.Images.Add(tb.fileName, tb.bmp);
ListViewItems[tb.fileName].ImageKey = tb.fileName;
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show(listView1.Items.Count + " Image(s) Imported.", "Import Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
button1.Enabled = true;
}
}
}
The most common method is to live with an SLOW first load but make thumbnails of the images and store them in the same folder with a different extension. Then subsequent runs can load the smaller file and execute faster. This way you only load the full size image when it is actually needed.