thecruz
asked on
How to add pictures to a listview control.
I'm adding the path to the video files to the listview control but i cant figure out how to add an image to each row or item. here is the code i'm using:
listView1.Items.Clear();
foreach (string files in Directory.GetFiles(dlg.Sel ectedPath) )
{
if ((files.EndsWith(".avi")) || files.EndsWith(".mpeg"))
{
listView1.Items.Add(files) ;
}
}
How can i add an image and only list the name of the file not the whole path?
Any help would be appreciated.
Thanks
listView1.Items.Clear();
foreach (string files in Directory.GetFiles(dlg.Sel
{
if ((files.EndsWith(".avi")) || files.EndsWith(".mpeg"))
{
listView1.Items.Add(files)
}
}
How can i add an image and only list the name of the file not the whole path?
Any help would be appreciated.
Thanks
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/876b6517-7306-44b0-88df-caebf3b1c10f
ASKER
This works when you're adding items manually. I'm trying to add them using the code above.
Don't forget to add picture in picturebox1, or if you have a high memory, try to uncomment //-getimage-
private void button?_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.ShowDialog();
if (dlg.SelectedPath == "") return;
foreach (string files in Directory.GetFiles(dlg.SelectedPath))
{
if ((files.EndsWith(".avi")) || files.EndsWith(".mpeg"))
{
ImageList Imagelist1 = new ImageList();
Imagelist1.ColorDepth = ColorDepth.Depth32Bit;
Imagelist1.ImageSize = new Size (100, 80);
listView1.View = View.LargeIcon;
listView1.LargeImageList = Imagelist1;
ListViewItem ThumbEntry = new ListViewItem();
ThumbEntry.Text = files;
Imagelist1.Images.Add(pictureBox1.Image);
//-getimage-
//Image i = Image.FromFile(files);
//Imagelist1.Images.Add(i.GetThumbnailImage(100, 80, null, IntPtr.Zero) );
ThumbEntry.ImageIndex = Imagelist1.Images.Count - 1;
listView1.Items.Add(ThumbEntry);
}
}
}
//-getimage-
//Image i = Image.FromFile(files);
//Imagelist1.Images.Add(i. GetThumbna ilImage(10 0, 80, null, IntPtr.Zero) );
Probably that will not work, because your dealing with video files not image files.
Instead, use the picturebox to load image in your listview video image.
//Image i = Image.FromFile(files);
//Imagelist1.Images.Add(i.
Probably that will not work, because your dealing with video files not image files.
Instead, use the picturebox to load image in your listview video image.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is working so far. Please look at the following code. I'm adding my images to a flowlayoutpanel control. when i select a folder that contains a significant amount of picture it crashes saying "Out of memory" is there anything i can do to fix it?
private void openFolderToolStripMenuIte m_Click(ob ject sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
flowLayoutPanel1.Controls. Clear();
foreach (string files in Directory.GetFiles(dlg.Sel ectedPath) )
{
GlovalVars.SelPath1 = dlg.SelectedPath;
if ((files.EndsWith(".jpg")) || files.EndsWith(".jpeg"))
{
FileStream st = new FileStream(files, FileMode.Open);
PictureBox pb = new PictureBox();
pb.SizeMode = PictureBoxSizeMode.Stretch Image;
pb.Width = 75;
pb.Height = 75;
pb.Image = Image.FromStream(st);
st.Close();
flowLayoutPanel1.Controls. Add(pb);
pb.Click += new EventHandler(view);
}
}
listView1.Items.Clear();
foreach (string files in Directory.GetFiles(dlg.Sel ectedPath) )
{
if ((files.EndsWith(".avi")) || files.EndsWith(".mpeg"))
{
ImageList Imagelist1 = new ImageList();
Imagelist1.ColorDepth = ColorDepth.Depth32Bit;
Imagelist1.ImageSize = new Size(25, 25);
listView1.View = View.LargeIcon;
listView1.LargeImageList = Imagelist1;
ListViewItem ThumbEntry = new ListViewItem();
FileInfo file = new FileInfo(files);
ThumbEntry.Text = file.Name;
Icon eIcon = System.Drawing.Icon.Extrac tAssociate dIcon(file s);
Imagelist1.Images.Add(eIco n);
ThumbEntry.ImageIndex = Imagelist1.Images.Count - 1;
listView1.Items.Add(ThumbE ntry);
}
}
}
}
private void openFolderToolStripMenuIte
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
flowLayoutPanel1.Controls.
foreach (string files in Directory.GetFiles(dlg.Sel
{
GlovalVars.SelPath1 = dlg.SelectedPath;
if ((files.EndsWith(".jpg")) || files.EndsWith(".jpeg"))
{
FileStream st = new FileStream(files, FileMode.Open);
PictureBox pb = new PictureBox();
pb.SizeMode = PictureBoxSizeMode.Stretch
pb.Width = 75;
pb.Height = 75;
pb.Image = Image.FromStream(st);
st.Close();
flowLayoutPanel1.Controls.
pb.Click += new EventHandler(view);
}
}
listView1.Items.Clear();
foreach (string files in Directory.GetFiles(dlg.Sel
{
if ((files.EndsWith(".avi")) || files.EndsWith(".mpeg"))
{
ImageList Imagelist1 = new ImageList();
Imagelist1.ColorDepth = ColorDepth.Depth32Bit;
Imagelist1.ImageSize = new Size(25, 25);
listView1.View = View.LargeIcon;
listView1.LargeImageList = Imagelist1;
ListViewItem ThumbEntry = new ListViewItem();
FileInfo file = new FileInfo(files);
ThumbEntry.Text = file.Name;
Icon eIcon = System.Drawing.Icon.Extrac
Imagelist1.Images.Add(eIco
ThumbEntry.ImageIndex = Imagelist1.Images.Count - 1;
listView1.Items.Add(ThumbE
}
}
}
}
//FileInfo file = new FileInfo(files); you dont need this
ThumbEntry.Text = files; //just like this
your possible error is here
>>pb.Image = Image.FromStream(st);
try to make a new image and assign it to pb.image
then close or dispose it before the loop again
ThumbEntry.Text = files; //just like this
your possible error is here
>>pb.Image = Image.FromStream(st);
try to make a new image and assign it to pb.image
then close or dispose it before the loop again
ASKER
How would i do that. Sorry to bug you but i'm new to this. The reason why i used the fileinfo is because i wanted to display the filename instead of the whole path.
Image im = Image.FromStream(st);
pb.Image = im.GetThumbnailImage(75, 75, null, IntPtr.Zero);
pb.Image = im.GetThumbnailImage(75, 75, null, IntPtr.Zero);
ASKER
that did it. you're the man. Thanks
ASKER
this is now still giving me more memory errors