Joan Venge
Joan Venge

Reputation: 331390

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?

Upvotes: 0

Views: 3570

Answers (3)

Skytracer
Skytracer

Reputation: 11

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)

Upvotes: 1

Jeff
Jeff

Reputation: 2248

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.

Upvotes: 0

pedrofernandes
pedrofernandes

Reputation: 16884

after your code try

listView1.Refresh();

Upvotes: 0

Related Questions