KdgDev
KdgDev

Reputation: 14529

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.

Upvotes: 6

Views: 1364

Answers (6)

Shadow Wizard
Shadow Wizard

Reputation: 66389

You probably can't speed things up as the bottle neck is reading the files themselves from disk and maybe parsing them as images.

What you can do though is Cache the list after it's loaded and then any subsequent calls to your code will be lot faster.

Upvotes: 0

Jake Pearson
Jake Pearson

Reputation: 27717

You could use Directory.EnumerateFiles along with Parallel.ForEach to spread the work over as many CPUs as you have.

var directory = "C:\\foo";
var files = Directory.EnumerateFiles(directory, "*.jpg");
var images = files.AsParallel().Select(file => Image.FromFile(file)).ToList();

Upvotes: 2

Vlad
Vlad

Reputation: 35584

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!

Upvotes: 2

dotalchemy
dotalchemy

Reputation: 2467

Given that you likely already know the path (from the dialog box?), you might be better using Directory.EnumerateFiles and then work with the collection it returns instead of a list.

http://msdn.microsoft.com/en-us/library/dd383458.aspx

[edit]

just noticed you're also loading the files into your app within the loop - how big are they? Depending on their size, it might actually be a pretty good speed!

Do you need to load them at this point? Can you change some display code elsewhere to load on demand?

Upvotes: 1

Ian Ringrose
Ian Ringrose

Reputation: 51897

As loading a image does both file IO and CPU work, you should get some speadup by using more then one thread.

If you are using .net 4, using tasks would be the way to go.

Upvotes: 1

jason
jason

Reputation: 241641

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?

Upvotes: 3

Related Questions