How to display images in listview C#(thumbnails proportionally)

Lionsure 2020-08-27 Original by the website

The C# listview control does not automatically zoom in or out when displaying images. To achieve proportional zoom, the images need to be drawn. Graphics is usually used in the winforms program to draw images, which is also a more efficient way to display thumbnails.

The article will use an example to illustrate the method displaying thumbnails of images in listview in C#, which is to display all the images in a specified folder in the form of thumbnails.

 

How to display images in listview C#(display thumbnails proportionally)

1. Drag a listview control to the Form, and set the DrawItem method of listview1 to listView1_DrawItem.

 

2. The implementation code is as follows:

private void imgThum_Load(object sender, EventArgs e)
       {
              ShowImg(@"G:\");
       }

/// <summary>
       /// How to display images in listview C#
       /// </summary>
       /// <param name="filePath">The path of folder where the image is located</param>

       private void ShowImg(string filePath)
       {
              this.listView1.OwnerDraw = true;
              this.listView1.TileSize = new Size(120, 120);
              this.listView1.View = View.Tile;

       DirectoryInfo di = new DirectoryInfo(filePath);
              FileInfo[] afi = di.GetFiles("*.*");
              string temp;
              ListViewItem lvi;
              for (int i = 0; i < afi.Length; i++)
              {
                     temp = afi[i].Name.ToLower();
                     if (temp.EndsWith(".jpg"))
                     {
                            lvi = new ListViewItem();
                            lvi.Text = afi[i].Name;
                            lvi.Tag = afi[i].FullName;
                            this.listView1.Items.Add(lvi);
                     }
                     else if (temp.EndsWith(".gif"))
                     {
                            lvi = new ListViewItem();
                            lvi.Text = afi[i].Name;
                            lvi.Tag = afi[i].FullName;
                            this.listView1.Items.Add(lvi);
                     }
                     else if (temp.EndsWith(".png"))
                     {
                            lvi = new ListViewItem();
                            lvi.Text = afi[i].Name;
                            lvi.Tag = afi[i].FullName;
                            this.listView1.Items.Add(lvi);
                     }
                     else if (temp.EndsWith(".bmp"))
                     {
                            lvi = new ListViewItem();
                            lvi.Text = afi[i].Name;
                            lvi.Tag = afi[i].FullName;
                            this.listView1.Items.Add(lvi);
                     }
              }
       }

private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
       {
              Bitmap bm = new Bitmap((string)(e.Item.Tag));
              e.Graphics.DrawImage(bm, e.Bounds);
              e.DrawText();
       }

The above code is tested by Visual Studio, and the thumbnail can be displayed correctly. However, if there are many images and the display is slow, you can consider enabling the VirtualMode of listview, but VirtualMode does not support Tile(tile view); other languages, such as VC++, can also be used.