2011-09-06 3 views
2

ListView 구성 요소를 사용하여 약 1,000 개의 이미지 축소판을 표시하려고하는데 성능 문제가 있습니다.ImageList가있는 ListView의 속도가 느린 이유는 무엇입니까? (다시 : 1000 + 축소판 그림)

먼저 1,000 개의 이미지가 포함 된 이미지 목록을 만듭니다. 이것은 번개가 빨리 걸리고 잠시 걸립니다. 내 ListView에로의 ImageList를 할당 한 후

그러나, 약 10 + 초 정도 걸립니다.

예 :

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

내가 성능을 향상시키기 위해 할 수있는 일이 있나요? My ImageList에는 이미 축소판 크기 (197x256 픽셀)로 크기가 조정 된 이미지가 포함되어 있으므로 문제가되지 않습니다. (내 ImageList를 만들 때 최대 1 초만 소요됩니다.)

+0

내 대답을 확인하고 컴퓨터에서 코드를 시험해보고로드 시간에 차이가 있는지 알려주십시오. –

답변

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; 
     } 
    } 
} 
관련 문제