简体   繁体   English

如何替换Winforms ImageList上的现有图像?

[英]How can I replace an existing image on a winforms ImageList?

How can I replace an existing image on a winforms ImageList ? 如何替换Winforms ImageList上的现有图像?

I tried this: 我尝试了这个:

this.CoolPics.Images [ 2 ] = // new image
this.ListViewControl.SmallImageList = this.CoolPics;

However the new image is not rescaled the way the others are, when I used the this.CoolPics.Images.Add method. 但是,当我使用this.CoolPics.Images.Add方法时,新图像不会像其他图像那样重新缩放。

What am I doing wrong? 我究竟做错了什么?

I know this is old but here is how I solved the problem. 我知道这很老,但是这是我解决问题的方法。 It looks like the image list will not resize the image upon assignment (even though it does when using the Add() function). 看起来图像列表在分配时不会调整图像的大小(即使使用Add()函数时也是如此)。 So basically, you need to resize the image manually before assigning. 因此,基本上,您需要在分配之前手动调整图像的大小。

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)

您的代码后尝试

listView1.Refresh();

I have run into this before and if I remember right the assignment operator had this behavior but the Imagelist.Images.Add(myImage) did the right thing. 我之前碰到过这个问题,如果我没记错的话,赋值运算符也有这种行为,但是Imagelist.Images.Add(myImage)做正确的事情。

Try changing your code to do the .Add(myImage) and see if that doesn't look better. 尝试更改代码以执行.Add(myImage),看看效果是否更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM