且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用新图像替换imagelist项目

更新时间:2022-10-23 11:04:19

看看 ObjectListView [ ^ ]



它让你想做的事情变得更容易:)



在你的情况下,我想我使用所有者绘图 [ ^ ]。



***的问候

Espen Harlinn


这是解决这个问题的方法:

1.按键从图像集中删除图像。

2.使用相同的键向图像集添加新图像。



图片newImage = ...  //  新图片 
string imgKey = ... // 要更新的图像的关键
// 找到上一张图片key
Image previousImage = imgList.Images [imgList.Images.Keys.IndexOf( imgKey 跨度>)];
// 从集合中删除上一张图片
imgList.Images.RemoveByKey (imgKey);
// 处理上一张图片
previousImage.Dispose();
// 添加新图片
imgList.Images.Add(imgKey, newImage);


You cannot vote on your own post
0

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:

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?

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


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.

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);