Answered by:
How to add image to listview on the fly

Question
-
Hi
Ii would like to add image to a list view on the fly while scaning a directory
i want the listview to be filled with file source and file image
i am able to add only file source to the list view how can i add the image as well
thanks
//here is my code
//loop all jpg files
foreach (string file in files) { ListViewItem item = new ListViewItem(file ); Add(item); } ////Liv_pictures is a listview control public void Add(ListViewItem item) { if (Liv_pictures.InvokeRequired) { Liv_pictures.BeginInvoke(new MethodInvoker(delegate { Add(item);//add jpg file async to listview using backgroundprocess })); } else { Liv_pictures.Items.Add(item); } }
- Edited by eyal1111 Wednesday, February 29, 2012 6:49 AM
Wednesday, February 29, 2012 6:49 AM
Answers
-
What is the problem? Whenever you call add passing the image file, you can add new Item to listView setting the ImageIndex. Like below,
foreach (string file in files) { Add(file); } private void Add(String file) { if(Liv_pictures.InvokeRequired) { Liv.pictures.BeginInvoke(new MethodInvoker(()=> Add(file))); } else { imageList.Images.Add(Image.FromFile(file)); ListViewItem item = new ListViewItem(file) item.ImageIndex = imageList.Images.Count-1; } }
Make sure that you have set LargeImageList and SmallImageList properties to imageList and View property to View.List.
I hope this helps.
Please mark this post as answer if it solved your problem. Happy Programming!
Wednesday, February 29, 2012 8:04 AM
All replies
-
You have to add the images to ImageList first and then set that imagelist to LargeImageList or SmallImageList properties of the listview. For example, in the below code, I create a image list and add all images from forlder to ImageList. Then I am adding items to listview setting the image index.
//ImageList to hold all Images ImageList Imagelist = new ImageList(); private void SampleForm_Load(object sender, EventArgs e) { //retrieve all image files String[] ImageFiles = Directory.GetFiles(@"C:\Users\am019318\Pictures\Wallpapers"); foreach (var file in ImageFiles) { //Add images to Imagelist Imagelist.Images.Add(Image.FromFile(file)); } //set the amall and large ImageList properties of listview listView1.LargeImageList = Imagelist; listView1.SmallImageList = Imagelist; //Create the Items in listview and set image index. for (int i = 0; i < Imagelist.Images.Count; i++) { listView1.Items.Add(new ListViewItem() { ImageIndex = i }); } }
You can refer this code and use it accoring to your need. I hope this helps.Please mark this post as answer if it solved your problem. Happy Programming!
Wednesday, February 29, 2012 7:39 AM -
Hi thaks for your answer ,
as i was saying i am not able to know in advanced how many files i will have
as you can see foreach loop
i call
Add(item);
which adds the listitem to the viewlist (onthefly)
so i can't use your code below:
foreach (var file in ImageFiles)
{
//Add images to Imagelist
Imagelist.Images.Add(Image.FromFile(file));
}
i need to add the image to the listview control on the flythanks
Wednesday, February 29, 2012 7:46 AM -
What is the problem? Whenever you call add passing the image file, you can add new Item to listView setting the ImageIndex. Like below,
foreach (string file in files) { Add(file); } private void Add(String file) { if(Liv_pictures.InvokeRequired) { Liv.pictures.BeginInvoke(new MethodInvoker(()=> Add(file))); } else { imageList.Images.Add(Image.FromFile(file)); ListViewItem item = new ListViewItem(file) item.ImageIndex = imageList.Images.Count-1; } }
Make sure that you have set LargeImageList and SmallImageList properties to imageList and View property to View.List.
I hope this helps.
Please mark this post as answer if it solved your problem. Happy Programming!
Wednesday, February 29, 2012 8:04 AM -
Hi eyal1111,
How is it going with Adavesh’s suggestions?
Would you mind letting us know the result?
In addition, could you please explain the words in your first reply? What’s the problem of Adavesh’s first code sample?
Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
Friday, March 2, 2012 2:40 AM -
Sorry aboutthe delay Adavesh’s suggestions was good i was abnle to solve the problem using his suggestion thanks
Saturday, March 24, 2012 12:30 PM