learner learner

learner learner

  • NA
  • 29
  • 155.4k

Replace imagelist item with a new image

Jan 29 2012 5:26 AM
I have filled an imagelist using the following code in a button click. everything works fine.
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: 
  // here I replace the image at a specific place
  imageList1.Images[listView1.FocusedItem.ImageIndex] = img; 
  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?