简体   繁体   中英

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

How can I replace an existing image on a 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.

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

Try changing your code to do the .Add(myImage) and see if that doesn't look better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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