Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speeding up the loading of a List of images

I'm loading a List<Image> from a folder of about 250 images. I did a DateTime comparison and it takes a full 11 second to load those 250 images. That's slow as hell, and I'd very much like to speed that up.

The images are on my local harddrive, not even an external one.

The code:

DialogResult dr = imageFolderBrowser.ShowDialog();
if(dr == DialogResult.OK) {

    DateTime start = DateTime.Now;

    //Get all images in the folder and place them in a List<>
    files = Directory.GetFiles(imageFolderBrowser.SelectedPath);
    foreach(string file in files) {
        sourceImages.Add(Image.FromFile(file));
    }
    DateTime end = DateTime.Now;

    timeLabel.Text = end.Subtract(start).TotalMilliseconds.ToString();
}

EDIT: yes, I need all the pictures. The thing I'm planning is to take the center 30 pixelcolums of each and make a new image out of that. Kinda like a 360 degrees picture. Only right now, I'm just testing with random images.

I know there are probably way better frameworks out there to do this, but I need this to work first.

EDIT2: Switched to a stopwatch, the difference is just a few milliseconds. Also tried it with Directory.EnumerateFiles, but no difference at all.

EDIT3: I am running .NET 4, on a 32-bit Win7 client.

like image 776
KdgDev Avatar asked Oct 28 '10 20:10

KdgDev


2 Answers

Do you actually need to load all the images? Can you get away with loading them lazily? Alternatively, can you load them on a separate thread?

like image 169
jason Avatar answered Sep 24 '22 02:09

jason


You cannot speed up your HDD access and decoding speed. However a good idea would be to load the images in a background thread.

Perhaps you should consider showing a placeholder until the image is actually loaded.

Caution: you'll need to insert the loaded images in your UI thread anyway!

like image 30
Vlad Avatar answered Sep 24 '22 02:09

Vlad