3

我对列表视图很陌生。我正在尝试在列表视图中显示图像。当我添加新项目时,所有以前的项目都会更改为新项目。所以列表视图具有相同的图像。我已经完成了谷歌搜索,但仍然找不到解决方案。请帮帮我。以下是我的代码。

ImageList imageList = new ImageList();

        OpenFileDialog opend1 = new OpenFileDialog();

        opend1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";

        opend1.Multiselect = true;

        if (opend1.ShowDialog() == DialogResult.OK)
        {

            listView1.View = View.LargeIcon;

            imageList.ImageSize = new Size(100, 100);

            for (int c = 0; c < opend1.FileNames.Length; c++)
            {

                Image i = Image.FromFile(opend1.FileNames[c].ToString());

                Image img = i.GetThumbnailImage(100, 100, null, new IntPtr());

                imageList.Images.Add(img);

            }

            listView1.LargeImageList = imageList;

            for (int j = 0; j < imageList.Images.Count; j++)
            {

                ListViewItem lstItem = new ListViewItem();

                lstItem.ImageIndex = j;

                listView1.Items.Add(lstItem);
            }

            listView1.Refresh();

        }
4

1 回答 1

1

在 WinForms 中使用ListView时,应将所有图像放入ImageList中,并仅提供该列表中图像的索引给您的ListViewItems.

listView1.LargeImageList = imageList1;
listView1.SmallImageList = imageList2;
listView1.Items.Add("Foo", 0);
listView1.Items.Add("Bar", 1);

创建和填充图像列表 id 设计器非常简单。您也可以手动进行:

imageList1.Images.Add(fooImage); // will have index 0
imageList1.Images.Add(barImage); // will have index 1

更新您的代码看起来不错。它是您应用程序中的确切代码吗?您只需要稍微简化一下:

listView1.View = View.LargeIcon;
imageList.ImageSize = new Size(100, 100);

foreach(var fileName in opend1.FileNames)
{
    Image img = Image.FromFile(fileName);
    imageList.Images.Add(img.GetThumbnailImage(100, 100, null, new IntPtr()));
}

listView1.LargeImageList = imageList;
for (int index = 0; index < imageList.Images.Count; index++)
     listView1.Items.Add(new ListViewItem() { ImageIndex = index });

listView1.Refresh();
于 2012-11-14T09:59:14.047 回答