C# 如何替换winforms ImageList上的现有映像?

C# 如何替换winforms ImageList上的现有映像?,c#,.net,winforms,imagelist,C#,.net,Winforms,Imagelist,如何替换winformsImageList上的现有图像 我试过这个: this.CoolPics.Images [ 2 ] = // new image this.ListViewControl.SmallImageList = this.CoolPics; 但是,当我使用this.CoolPics.Images.Add方法时,新图像没有像其他图像那样重新缩放 我做错了什么?请在您的代码后重试 listView1.Refresh(); 我以前遇到过这种情况,如果我没记错的话,赋值操作符有这种

如何替换winforms
ImageList
上的现有图像

我试过这个:

this.CoolPics.Images [ 2 ] = // new image
this.ListViewControl.SmallImageList = this.CoolPics;
但是,当我使用
this.CoolPics.Images.Add
方法时,新图像没有像其他图像那样重新缩放

我做错了什么?

请在您的代码后重试

listView1.Refresh();

我以前遇到过这种情况,如果我没记错的话,赋值操作符有这种行为,但是Imagelist.Images.Add(myImage)做了正确的事情


尝试更改代码以执行.Add(myImage)操作,看看效果是否更好。

我知道这很旧,但下面是我解决问题的方法。看起来图像列表在赋值时不会调整图像大小(即使在使用Add()函数时也是如此)。因此,基本上,您需要在指定之前手动调整图像大小

Image img; //used to load new image from disk
Bitmap bmp = new Bitmap(160, 120); //canvas where the new image will be drawn/resized
Graphics graph = Graphics.FromImage(bmp); //used to draw/resize the new image

img = new Bitmap(fileDialog.FileNames[0]); //load new image from disk

graph.DrawImage(img, new Rectangle(0, 0, 160, 120)); //resize new image to proper size

imgList.Images[index] = bmp; //assign the new resized image to the list (overwrites the old image)

谢谢,我今天就试试。我刚试过,但没用。它更改了图像,但图像没有像我第一次添加图像时那样重新缩放。谢谢,唯一的问题是我必须删除旧图像,这将更改其他listview项目(?)我不知道为什么imagelist ImageCollection有RemoveAt而没有Insert。似乎缺少功能。Total hack,但您可以添加它,保存Add的返回索引,使用=将您想要的索引分配给新索引,然后使用RemoveAt将添加的索引与保存的索引一起删除。。。