question

Siwek44-8026 avatar image
0 Votes"
Siwek44-8026 asked JackJJun-MSFT commented

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

 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?



dotnet-csharpwindows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Siwek44-8026, is any update? I think Castorix31's answer is good, how about you?

0 Votes 0 ·

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered

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



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.