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?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,870 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,603 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 83,021 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