Solved

How to add pictures to a listview control.

Posted on 2010-08-26
11
447 Views
Last Modified: 2013-12-17
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.SelectedPath))
                {
                    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
0
Comment
Question by:thecruz
  • 5
  • 5
11 Comments
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33533921
0
 

Author Comment

by:thecruz
ID: 33534670
This works when you're adding items manually.  I'm trying to add them using the code above.
0
 
LVL 14

Expert Comment

by:systan
ID: 33538808
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);

                }

            }

        }

        

Open in new window

0
 
LVL 14

Expert Comment

by:systan
ID: 33538836
//-getimage-
//Image i = Image.FromFile(files);
//Imagelist1.Images.Add(i.GetThumbnailImage(100, 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.
0
 
LVL 14

Accepted Solution

by:
systan earned 500 total points
ID: 33538923
OR if you really really want the video image icon? Here it is;
        private void button6_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;



                    Icon eIcon = System.Drawing.Icon.ExtractAssociatedIcon(files);

                    Imagelist1.Images.Add(eIcon);



                    ThumbEntry.ImageIndex = Imagelist1.Images.Count - 1;

                    listView1.Items.Add(ThumbEntry);

                }

            }

        }

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:thecruz
ID: 33545214
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 openFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                flowLayoutPanel1.Controls.Clear();

                foreach (string files in Directory.GetFiles(dlg.SelectedPath))
                {
                    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.StretchImage;
                        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.SelectedPath))
                {
                    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.ExtractAssociatedIcon(files);
                        Imagelist1.Images.Add(eIcon);

                        ThumbEntry.ImageIndex = Imagelist1.Images.Count - 1;
                        listView1.Items.Add(ThumbEntry);
                    }
                }
            }
        }
0
 
LVL 14

Expert Comment

by:systan
ID: 33549646
//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
0
 

Author Comment

by:thecruz
ID: 33549945
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.
0
 
LVL 14

Expert Comment

by:systan
ID: 33550512
Image im = Image.FromStream(st);
pb.Image = im.GetThumbnailImage(75, 75, null, IntPtr.Zero);



0
 

Author Comment

by:thecruz
ID: 33550608
that did it.  you're the man.  Thanks
0
 

Author Comment

by:thecruz
ID: 33653739
this is now still giving me more memory errors
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Contant UI messages in .NET 1 40
Setting location of a form 4 22
Format column on datatable 7 17
XML & .net 5 18
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now