Take the 2-minute tour ×

I have to combine lets say about 100 images (png-files). The problem is not combining them, that runs quick enough. But loading them from storage takes up to 4sec. That is too much time.

So I can use TPL or multiple threads but it is still too slow. How can I speed it up? To hold all the images in main storage is not an option unfortunately.

The images are quite small: from 4KByte to 10KByte

I'm loading the images that way:

Image img = Image.FromFile(file);
share|improve this question
how large are the images? –  Brian Apr 10 '12 at 13:14
I would use a profiler to see where it actually is slow. Could be I/O as well as some other code. (I use ANTS Performance Profiler for such tasks) –  Uwe Keim Apr 10 '12 at 13:15
sound like you are being limited by your hardware, try the profiler that Uwe suggested, if it doesn't reveal anything then you will simply need a faster HDD/SSD –  Eamonn McEvoy Apr 10 '12 at 13:24
add comment

3 Answers

up vote 1 down vote accepted

Two ideas:

  • If the bulk of the lag really is from IO, compress the files. Depending on their contents, even a simple ZIP compression could reduce their size, thus less bytes to read. The work will be to decompress them in memory. I don't know if that's applicable for your case

  • Lazy load them. Do you need all 100 images loaded all the time? Perhaps you can just load the first ones, or the most important ones first, let the software do the other stuff while it finishes loading the remaining images in the background.

share|improve this answer
Since they seem to be PNG files, it is unlikely that ZIP compression would be of any use. It could certainly help if all images were compressed into a single ZIP file, though, but I doubt that it is feasible (since manton probably doesn't have any control on source data). –  Lâm Tran Duy Apr 10 '12 at 14:55
@LâmTranDuy PNG is lightly compressed, but even then the contents will say if compression is worthwhile. Try compressing a white rectangle. I get a ZIP that's 20% the size of the PNG. –  MPelletier Apr 10 '12 at 15:00
Interesting, since the PNG format is supposed to rely on a variation of the LZ77 compression algorithm, which is the algorithm used by the ZIP format. I guess there are enough variations in the two algorithms to get such different results in some specific scenarios. –  Lâm Tran Duy Apr 10 '12 at 15:10
@MPelletier I'm going to try your both proposals. I think this is the best I can do. –  manton Apr 10 '12 at 15:45
@manton I'd prefer that you find out if anything I suggest can indeed work and does a better job before you accept my answer, but thanks and good luck. It's an interesting project, and I find optimization to always be a fun challenge. –  MPelletier Apr 10 '12 at 15:50
show 1 more comment

Creating multiple threads does not improve I/O speed in your case. That is about your harddisk's read-write speed.

Loading 100 high quality images in 4 seconds seems normal.

share|improve this answer
The files are really small: from 4KByte to 10KByte. –  manton Apr 10 '12 at 14:38
Small files are more fragmented on the disk. This may cause bad seek time and rotational delay. What happens if you run it with single thread and multiple threads? Does the result change? –  Mert Apr 10 '12 at 14:46
In some cases the duration increases and in other cases the time gets smaller but not significantly. –  manton Apr 10 '12 at 14:56
add comment

How do you load your image files? Please share a piece of your code. My guess is that you read not whole file at once but something like byte by byte until the EOF... One of ways to optimize file loading is to load whole file into preallocated memory buffer.

share|improve this answer
That is the code: Image img = LoadImgFromFile(CurDirImg + imgNameAr[i]); What exactly is preallocated memory buffer and how can I use it in .net? –  manton Apr 10 '12 at 14:40
LoadImgFromFile() it not a .NET Framework method. Can you post the source code for that function ? Also, why are you using it instead of the standard Image.FromFile() method ? –  Lâm Tran Duy Apr 10 '12 at 14:51
@LâmTranDuy Sorry. This is the code: Image img = Image.FromFile(file); –  manton Apr 10 '12 at 14:57
add comment

Your Answer


