Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
You cannot vote on your own post
0

I have filled an imagelist using the following code in a button click. everything works fine.

C#
DirectoryInfo dir = new DirectoryInfo(@"c:\MyPic");
foreach (FileInfo file in dir.GetFiles())
{
imageList1.Images.Add(Image.FromFile(file.FullName));
}

listView1.View = View.LargeIcon;
imageList1.ImageSize = new Size(100, 100);
listView1.LargeImageList = this.imageList1;
ListViewItem item;
for (int i = 0; i < this.imageList1.Images.Count; i++)
{
    item = new ListViewItem();
    item.ImageIndex = i;
    item.Text = "Image " + i.ToString();
    listView1.Items.Add(item);
}



But when i replace any image in the imagelist and refresh the list box then the full image is not displayed, a portion of the image is displayed. Codes are as follows:

C#
imageList1.Images[listView1.FocusedItem.ImageIndex] = img;  // here I replace the image at a specific place
 
listView1.Refresh();


I think I have to change the imagelayout as stretch. But how could i do that? and why the full image is displayed at first time?
Posted
Updated 28-Jan-12 23:34pm
v2
Comments
Michel [mjbohn] 29-Jan-12 5:38am    
code tags added
Manisha Tambade 29-Jan-12 6:03am    
hi,Same thing happened with me but i was using imagelist by drag n drop method and not by coding like you..y
At the end i found image file size difference.So u check file size of both image,hope so u get problem solved..

Have a look at ObjectListView[^]

It makes what you are trying to do a lot easier :)

In your case I think I'd use owner drawing [^].

Best regards
Espen Harlinn
 
Share this answer
 
Comments
akul123 29-Jan-12 8:23am    
since at the first time it can be done using ListView so why it can not be possible in case of replacing. I think in case of replacing somehow any property of listview or imagelist has been changed that I have to rechange.
Espen Harlinn 29-Jan-12 8:31am    
An imagelist is basically a large image composed of smaller images - adding and removing those smaller images from the larger image is not particularly efficient.
This is the way how to solve this problem:
1. Remove the image from the image collection by key.
2. Add a new image with the same key to the image collection.

C#
Image newImage = ... // new image
string imgKey = ... // the key of the image to update
// find the previous image by key
Image previousImage = imgList.Images[imgList.Images.Keys.IndexOf("imgKey")];
// remove the previous image from the collection
imgList.Images.RemoveByKey(imgKey);
// dispose the previous image
previousImage.Dispose();            
// add a new image
imgList.Images.Add(imgKey, newImage);
 
Share this answer
 
Comments
CHill60 15-Dec-15 17:25pm    
Question is nearly 4 years old! And I'm not convinced that this solution would not just produce the same problem the OP had

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900