How to display images with different sizes with keeping their aspect ratio in listview?

Siwek44 1 Reputation point
2022-05-06T23:28:20.25+00:00
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.IsFolderPicker = true;
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                textBox1.Text = dialog.FileName;
            }

            string filePath = dialog.FileName;

            this.listView1.OwnerDraw = true;
            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++)
            { 
                if (Path.GetExtension(afi[i].ToString()) == ".jpg" || Path.GetExtension(afi[i].ToString()) == ".png" || Path.GetExtension(afi[i].ToString()) == ".gif" || Path.GetExtension(afi[i].ToString()) == ".bmp")
                {
                    var img = Image.FromFile(filePath + @"\" + afi[0]);
                    int width = img.Width;
                    int height = img.Height;

                    this.listView1.TileSize = new Size(width, height);

                    temp = afi[i].Name.ToLower();
                    if (temp.EndsWith(".jpg"))
                    {
                        lvi = new ListViewItem();
                        lvi.Text = i.ToString();
                        lvi.Tag = afi[i].FullName;
                        this.listView1.Items.Add(lvi);

                    }
                    else if (temp.EndsWith(".png"))
                    {
                        lvi = new ListViewItem();
                        lvi.Text = i.ToString();
                        lvi.Tag = afi[i].FullName;
                        this.listView1.Items.Add(lvi);
                    }
                    else if (temp.EndsWith(".gif"))
                    {
                        lvi = new ListViewItem();
                        lvi.Text = i.ToString();
                        lvi.Tag = afi[i].FullName;
                        this.listView1.Items.Add(lvi);
                    }
                    else if (temp.EndsWith(".bmp"))
                    {
                        lvi = new ListViewItem();
                        lvi.Text = i.ToString();
                        lvi.Tag = afi[i].FullName;
                        this.listView1.Items.Add(lvi);
                    }

                }

            }

this.listView1.TileSize = new Size(width, height); sets a given size for all images in listview, how can i do this separately for each of them?

Developer technologies Windows Forms
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2022-05-07T08:43:37.387+00:00

    A way is to get a Thumbnail and resize it

    I tested with IThumbnailProvider instead of Image.GetThumbnailImage
    and with big and small images to check the ratio =>

    199826-listview-images.gif

    I cannot post the test code with those bugged forums, then a link : ListView with scaled images

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.