rainbower
rainbower

Reputation: 141

How to Reduce the Memory usage of ImageList

Im using the following code to fetch image from a file,create an image list by adding all the files from a folder and finally linking it to a listview control to display the thumbnails.The Problem is that if i add 300 Images,the program uses more than 700MB of memory.The image list is taking a lot of memory.Is there any way i can compress/rescale the images at runtime to reduce the memory usage or is there any alternative.

                        this.t.Images.Add(Image.FromFile(f));
                        Filelist.Items.Add(f.ToString());
                        ListViewItem item = new ListViewItem();
                        this.listview.Items.Add(item);

Upvotes: 1

Views: 1818

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 133975

Load the image into a temporary, resize it to a new image, and then save the resized image in the list.

using (var tempImage = Image.FromFile(f))
{
    Bitmap bmp = new Bitmap(thumbnailWidth, thumbnailHeight);
    using (Graphics g = Graphics.FromImage(bmp))
    {
        g.DrawImage(tempImage, new Rectangle(0, 0, bmp.Width, bmp.Height);
    }
    t.Images.Add(bmp);
}

Upvotes: 4

Related Questions