2

我正在尝试使用 ListView 组件来显示大约 1,000 个图像缩略图,但遇到了一些性能问题。

首先,我创建一个包含 1,000 张图像的 ImageList。这是闪电般的速度,只需不到一秒钟。

但是,一旦我将 ImageList 分配给我的 ListView,大约需要 10 多秒。

例子:

ImageList _imgList = GetMyImageList();  // takes under 1 second
ListView _lstView = new ListView();
lstView.LargeImageList = _imgList; // takes 10+ seconds

我能做些什么来提高性能吗?我的 ImageList 包含已经调整为缩略图大小(197x256 像素)的图像,所以这不是问题......(创建我的 ImageList 最多只需要 1 秒)。

4

1 回答 1

0

列表视图中的数据是否经常更改?您是否经常加载新的图像列表?

我尝试了您的场景并获得了几秒钟的加载时间(因为我正在生成随机图像),但是在更改列表视图[View]模式以及滚动时刷新时间非常快。

这是示例代码。试试看,让我知道它是如何工作的。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication10
{
    public partial class FormListView:
        System.Windows.Forms.Form
    {
        public FormListView ()
        {
            string [] names = null;

            this.InitializeComponent();

            names = Enum.GetNames(typeof(View));
            for (int i=0; i < names.Length; i++)
            {
                this.comboBox1.Items.Add(names [i]);

                if (names [i] == this.ListView.View.ToString())
                    this.comboBox1.SelectedIndex = i;
            }
        }

        private void comboBox1_SelectedIndexChanged (object sender, EventArgs e)
        {
            this.ListView.View = (View) Enum.Parse(typeof(View), this.comboBox1.SelectedItem.ToString());
            this.ListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        private void ButtonLoadImages_Click (object sender, System.EventArgs e)
        {
            Image image;
            Stopwatch watch;

            this.Enabled = false;
            this.Cursor = Cursors.WaitCursor;

            this.ListView.SmallImageList = null;
            this.ListView.LargeImageList = null;
            this.ListView.StateImageList = null;

            while (this.ImageList.Images.Count > 0)
            {
                this.ImageList.Images [0].Dispose();
                this.ImageList.Images.RemoveAt(0);
            }

            this.ImageList.ImageSize = new System.Drawing.Size(256, 256);

            watch = Stopwatch.StartNew();
            for (int i=0; i < 1000; i++)
            {
                image = new Bitmap(this.ImageList.ImageSize.Width, this.ImageList.ImageSize.Height);
                using (Graphics graphics = Graphics.FromImage(image))
                {
                    graphics.Clear(Color.White);
                    graphics.DrawRectangle(Pens.Red, 10, 10, this.ImageList.ImageSize.Width - 20, this.ImageList.ImageSize.Height - 20);
                    graphics.DrawString(i.ToString(), this.Font, Brushes.Blue, 20, 20);
                }
                this.ImageList.Images.Add(image);
            }
            watch.Stop();

            this.ListView.SmallImageList = this.ImageList;
            this.ListView.LargeImageList = this.ImageList;
            this.ListView.StateImageList = this.ImageList;

            this.Text = watch.Elapsed.TotalSeconds.ToString();

            this.Cursor = Cursors.Default;
            this.Enabled = true;
        }

        private void ButtonLoadItems_Click (object sender, System.EventArgs e)
        {
            Stopwatch watch;
            ListViewItem item;

            this.Enabled = false;
            this.Cursor = Cursors.WaitCursor;

            this.ListView.Items.Clear();
            this.ListView.Columns.Clear();
            this.ListView.Columns.Add("Id", "Id");
            this.ListView.Columns.Add("Name", "Name");

            this.ListView.SmallImageList = null;
            this.ListView.LargeImageList = null;
            this.ListView.StateImageList = null;

            this.ListView.BeginUpdate();
            watch = Stopwatch.StartNew();
            for (int i=0; i < 1000; i++)
            {
                item = new ListViewItem();

                item.ImageIndex = i;
                item.Text = i.ToString();
                item.SubItems.Add("qwerty");

                this.ListView.Items.Add(item);
            }
            this.ListView.EndUpdate();

            this.ListView.SmallImageList = this.ImageList;
            this.ListView.LargeImageList = this.ImageList;
            this.ListView.StateImageList = this.ImageList;

            this.ListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);

            watch.Stop();

            this.Text = watch.Elapsed.TotalSeconds.ToString();

            this.Cursor = Cursors.Default;
            this.Enabled = true;
        }
    }
}
于 2013-05-23T07:59:33.923 回答