c# - 如何更改 Listview 的现有 Imagelist 图像大小

标签 c#

我试图在 ListView 处于事件状态并包含项目时更改图像列表图像大小。但似乎只影响新添加的图像,现有图像会变成空白。

这是我的代码:

public Form1()
{
    ...
    imglst_ = new ImageList();
    imglst_.ImageSize = new Size(80, 80);
    listView1.SmallImageList = imglst_;
    listView1.LargeImageList = imglst_;
    ...
}

//zoom in
//This code only affect the new added image
//the existing images will become blank
private void toolStripZoomin_Click(object sender, EventArgs e)
{
    int w = imglst_.ImageSize.Width;
    int h = imglst_.ImageSize.Height;

    w = (int)(w * 1.2);
    h = (int)(h * 1.2);

    imglst_.ImageSize = new Size(w, h);
}

最佳答案

ImageSize 的文档属性状态:

Setting the ImageSize property prior to adding images to the image collection causes the images to be resized to the image size specified.

When you set the ImageSize property to a new value, the Handle for the image list is recreated.

Because setting the ImageSize property causes the handle to be recreated, you should set ImageSize prior to setting the Images property. When the handle for the ImageList has been created, setting the ColorDepth or ImageSize properties in code, after setting the Images property, will cause the collection of images set for the Images property to be deleted.

所以添加图片后不能更改ImageSize,应在设置ImageSize后再次将图片添加到ImageList中因此它们将以新指定的尺寸绘制。

关于c# - 如何更改 Listview 的现有 Imagelist 图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43301283/

相关文章:

c# - 取消异步操作

c# - 是否有 C# 的在线临时代码库? (JS Bin,jsFiddle 克隆)?

c# - 获取错误 "The name ' 配置'在当前上下文中不存在”

c# - 统一构建后在数据文件夹中复制一些额外的文件

c# - 使用 XAML 和 Xamarin 将母版页注入(inject)主从页面

c# - CloudTableClient客户端超时

c# - LibTiff.NET Tiff 到 WPF 图像

c# - 单击按钮后回发后如何显示 div?

c# - 如何在 C# 中组合部分类对象?

c# - 具有多种字体属性的文本 block 文本