【问题标题】:display image[] in listview?在列表视图中显示图像 []?
【发布时间】:2013-05-18 23:17:38
【问题描述】:

我想在列表视图中显示Image[],但不知道如何操作。我在网上搜索,一致认为ImageList 是首选。它可以正常工作和显示,但不适合我的需要。

如何将此代码转换为使用Image[] 而不是ImageList

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{

     string dir = (string)e.Argument;
     imageArr = Directory.GetFiles(dir, "*.jpeg", SearchOption.AllDirectories);

     picList = new ImageList();

     toolStripStatusLabel1.Text = "Generating thumbnails...";

     for (int i = 0; i < imageArr.Length; i++)
     {
         Image img = Image.FromFile(imageArr[i]);
         int width = img.Width / 10;
         int height = img.Height / 10;
         Image pic = img.GetThumbnailImage(width, height, null, new IntPtr());
         picList.Images.Add(pic);

         int procent = ((i + 1) * 100) / imageArr.Length;
         bw.ReportProgress(procent);
     }

     bw.ReportProgress(0);
     toolStripStatusLabel1.Text = "";

     populateListView(picList);
 }

 private delegate void populateListViewDelegate(ImageList picList);

 private void populateListView(ImageList picList)
 {
     if (InvokeRequired)
     {
         this.Invoke(new populateListViewDelegate(populateListView), picList);
         return;
     }

     Size thumbSize = new Size(200, 141);
     listView1.View = View.LargeIcon;
     picList.ImageSize = thumbSize;
     listView1.LargeImageList = picList;

     toolStripStatusLabel1.Text = "Adding thumbnails to listview...";

     for (int i = 0; i < picList.Images.Count; i++)
     {
         ListViewItem item = new ListViewItem();
         item.ImageIndex = i;
         listView1.Items.Add(item);

         int procent = ((i + 1) * 100) / picList.Images.Count;
         bw.ReportProgress(procent);
     }

     bw.ReportProgress(0);
     toolStripStatusLabel1.Text = "";
 }

显然int widthint height 不起作用,因为 ImageList 对所有图像都有相同的大小,如果我理解正确的话,但我当时不知道,这就是我懒惰的自我离开的原因代码在那里。

基本上,我前面提到的“需求”是这些“缩略图”应该能够有不同的大小。

对解决方法有任何帮助,关于我的问题的提示或者我应该采取不同的方法吗?

【问题讨论】:

  • 如果您需要显示不同大小的缩略图,ListView 将无济于事——ListView 无法显示不同高度的行。如果您只显示图像,那么 ListBox 将更适合您的需求。
  • 但是理论上,图像可以具有不同的大小,而行的大小是统一的。不能对缩略图的大小进行限制,以使其不超过行的大小吗?我需要一次显示几千个缩略图,最好大小应该能够不同,但差异会非常小,例如由于方向而导致的差异;横向纵向。关于如何/做什么的任何提示?

标签: c# winforms image listview


【解决方案1】:

您需要在添加图片之前设置ImageSize。如果您更改ImageSize,任何以前添加的图像都会被删除。 See the Remarks here

您可能还想在添加任何图像之前将 ColorDepth 设置为 32 位。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-26
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    相关资源
    最近更新 更多