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

发布于 2024-07-24 14:23:09 字数 273 浏览 10 评论 0原文

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

我尝试了这个:

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

但是,当我使用 this.CoolPics.Images.Add 方法时,新图像不会像其他图像那样重新缩放。

我究竟做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十雾 2024-07-31 14:23:10

我知道这已经过时了,但这就是我解决问题的方法。 看起来图像列表不会在分配时调整图像大小(即使在使用 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)

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)
|煩躁 2024-07-31 14:23:10

在你的代码尝试之后

listView1.Refresh();

after your code try

listView1.Refresh();
野鹿林 2024-07-31 14:23:10

我以前遇到过这个问题,如果我没记错的话,赋值运算符有这种行为,但 Imagelist.Images.Add(myImage) 做了正确的事情。

尝试更改代码以执行 .Add(myImage) 并查看是否看起来更好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文