Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image.FromFile is very SLOW. Any alternatives, optimizations?

I have a winforms image list which contains say like 200 images 256x256.

I use the method Images.FromFile to load the images and then add them to the image list.

According to ANTS .NET profiler, half of the program's time is spent in Images.FromFile. Is there a better way to load an image to add to an image list?

Another thing that might be optimized is, the images that are loaded are larger than 256x256. So is there a way to load them by resizing them first or something? I just want to uniform scale them if they their height is larger than 256 pixels.

Any idea to optimize this?

EDIT: They are JPEGs.

like image 239
Joan Venge Avatar asked Dec 29 '22 21:12

Joan Venge


2 Answers

You don't say how much bigger than 256x256 the images actually are - modern digital cameras images are much bigger than this.

Disk I/O can be very slow, and I would suggest you first get a rough idea how many megabytes of data you're actually reading.

Then you can decide if there's a subtle 'Image.FromFile' problem or a simple 'this is how slow my computer/drives/anti-virus scanner/network actually is' problem.

A simple test of the basic file I/O performance would be do to File.ReadAllBytes() for each image instead of Image.FromFile() - that will tell you what proportion of the time was spent with the disk and what with the image handling - I suspect you'll find it's largely disk, at which point your only chance to speed it up might be one of the techniques for getting JFIF thumbnails out of files. Or perhaps one can imagine clever stuff with partial reads of progressive JPEGs, though I don't know if anyone does that, nor if your files are progressive (they're probably not).

I don't really know how fast you need these to load, but if the problem is that an interactive application is hanging while you load the files, then think of ways to make that better for the user - perhaps use a BackgroundWorker to load them asynchronously, perhaps sort the images by ascending file-size and load the small ones first for a better subjective performance.

like image 91
Will Dean Avatar answered Jan 01 '23 09:01

Will Dean


If you are trying to make thumbnails then try this code here it will let you extract thumbnails without completely loading the image.

like image 38
murasaki5 Avatar answered Jan 01 '23 09:01

murasaki5