c# - 将图像添加到 ListView

标签 c# listview imagelist

我有这个问题。我想将图像添加到listView。我确实想使用 openFileDialog 选择光盘上的图像,将文件加载到应用程序并在 listView 中显示它们。

现在我这样做:

        openFileDialog1.Filter = "png (*.png)|*.png";
        openFileDialog1.Multiselect = true;

        if ( openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {    
            string[] files = openFileDialog1.FileNames;

            foreach ( var pngFile in files ) {
                try {
                    Bitmap image = new Bitmap( pngFile );
                    imageList1.Images.Add( image );
                } catch {
                }
            }
            listView1.LargeImageList = imageList1;
            listView1.Refresh();
        }

但是这不起作用。我做错了什么?

编辑

我得到空白 ListView 。没有任何错误。

最佳答案

嗯,没关系。但您只将图像添加到图像列表中。您尚未修改 ListView 中实际使用该添加图像的项目。添加此行代码并根据需要进行调整:

  listView1.Items.Add(new ListViewItem("Added an image", imageList1.Images.Count - 1));

还要确保 listView1.LargeImages = imageList1。您可以在设计器中进行设置。

关于c# - 将图像添加到 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6955324/

相关文章:

c# - 在 C# 中使用多个分隔符拆分字符串

java - ListView 不显示

android - TabLayout header 隐藏 fragment 中 ListView 上的第一项

c++ - ImageLists for ListViews 中的透明图像

c# - 具有/不同配置值的单元测试静态构造函数

c# - 如何在 WPF 中为 TranslateTransform 和 ScaleTransform 设置动画

c# - 自动映射类型为接口(interface)的属性

vb.net - ListView AutoReziseColumns 不起作用

C# ListView 图像在运行时不显示