views:

343

answers:

1

how to create fixed size thumbnail dynamically and resize image in listview best fit the size of the thumbnail.

private void Treeview1_AfterSelect(System.Object sender, System.Windows.Forms.TreeViewEventArgs e)
{
    if (folder != null && System.IO.Directory.Exists(folder))
                {
                    try
                    {

                        DirectoryInfo dir = new DirectoryInfo(@folder);
                        foreach (FileInfo file in dir.GetFiles())
                        {
                            try
                            {
                                imageList.ImageSize = new Size(136, 136);
                                imageList.ColorDepth = ColorDepth.Depth32Bit;
                                Image img = new Bitmap(Image.FromFile(file.FullName));                                    
                                Graphics g = Graphics.FromImage(img);

                                g.DrawRectangle(Pens.Red, 0, 0, img.Width - 21, img.Height - 21);
                                //g.DrawRectangle(Pens.Red, 0, 0, img.Width, img.Height);                                                                                                                                                
                                imageList.Images.Add(img);
                            }
                            catch
                            {
                                Console.WriteLine("This is not an image file");
                            }     
                        }


                        for (int j = 0; j < imageList.Images.Count; j++)
                        {
                            this.ListView1.Items.Add("Item" + j);
                            this.ListView1.Items[j].ImageIndex = j;
                        }

                        this.ListView1.View = View.LargeIcon;
                        this.ListView1.LargeImageList = imageList;
                        //this.ListView1.DrawItem += new DrawListViewItemEventHandler(ListView1_DrawItem);                                                       

                        //import(folder);
                    }
                    catch (Exception ex)
                    {
                    }
}
A: 

I have code in this answer specifically for resizing images along with resolution.

rick schott