Speeding up image list loading

I am loading a List<Image> from a folder about 250 images in size. I did a DateTime comparison, and it takes only 11 seconds to download these 250 images. It’s slow, damn it, and I would really like to speed it up.

Images are on my local hard drive, not even on 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. What I'm planning is to take a center of 30 pixels for each of them and make a new image out of it. Kinda like a 360 degree image. Only now I'm just testing with random images.

I know that there is probably a better framework for this, but I need this to work in the first place.

EDIT2: switches to a stopwatch, the difference is only a few milliseconds. Also tried it with Directory.EnumerateFiles, but there was no difference at all.

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

+6
c # generic-list
source share
6 answers

As image loading does the work of both IO and CPU, you should get some speadup using more than one thread.

If you are using .net 4, using tasks will be a way.

+1
source share

Do you really need to upload all the images? Can you easily download them? Alternatively, can you upload them to a separate stream?

+3
source share

You will not be able to speed up the speed of access to the hard disk and the speed of its decoding. However, it would be a good idea to upload images to the background stream.

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

Note: you will need to embed the downloaded images into the user interface stream anyway!

+2
source share

You can use Directory.EnumerateFiles with Parallel.ForEach to extend the work to as many processors as you have.

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

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

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

[edit]

just noticed that you also upload files to your application in a loop - how big are they? Depending on their size, this can be a pretty good speed!

Do you need to download them at this moment? Can you change the display code elsewhere for download on demand?

+1
source share

You probably cannot speed things up, as the neck of the bottle reads files from the disk and, possibly, analyzes them as images.

What you can do is Cache the list after loading it, and then any subsequent calls to your code will be much faster.

0
source share

All Articles