Image.FromFile alternative

AdeHall

Member
Joined
Feb 24, 2003
Messages
11
Im trying to load multiple jpegs from disk to create thumbnails. Ive tried various ways but the Image.FromFile( path ) or Bitmap aBitmap = new Bitmap( path ) methods seem very slow.

The jpegs are about 750k each but it is taking approx 30-40 seconds to load 100.

Are there any faster ways to load the data?
Thanks
Ade
 
Youre probably running out of memory and having to swap to disk. Are you disposing of each image and any intermediate bitmaps and graphics objects before loading the next?
 
This is a little benchmark prog I tried, to make sure that it was the IO that was taking the time before I do anything with the image.
I ended up forcing the garbage collector because as you say I was running out of memory.

When you use one of the commercial photo apps out there they seem to be able to read in and convert the same images in a fraction of the time. Surely .NET cant be making that much difference can it?

C#:
if ( openFileDialog1.FileNames != null )
				{
					DateTime StartTime = DateTime.Now;
					System.Diagnostics.Debug.WriteLine( "Start:" + StartTime.ToLongTimeString());
										int i=0;
					foreach ( string FileName in openFileDialog1.FileNames )
					{
						i++;
						Bitmap _bitmapNew;
						 _bitmapNew = (Bitmap)Image.FromFile( FileName, false );
											_bitmapNew = null;
						GC.Collect();
						GC.WaitForPendingFinalizers();
					}
					DateTime EndTime = DateTime.Now;
					System.Diagnostics.Debug.WriteLine( "End:" + EndTime.ToLongTimeString());
					System.Diagnostics.Debug.WriteLine( "Loading " + i.ToString() + " Photos took " + (EndTime - StartTime) ) ;
				}
}
 
Theres no need to invoke the garbage collector like that. In my last post I said you need to dispose of objects when youre done with them. In that example youre still not doing this.

C#:
Bitmap _bitmapNew;
_bitmapNew = (Bitmap)Image.FromFile( FileName, false );
_bitmapNew.Dispose();
_bitmapNew = null;

Dont worry too much if you see your memory usage increasing - if it becomes a problem, or after a short delay, the garbage collector will collect.
 
Thanks Divil

The memory as you say is looking after itself better.
However, it is still taking 45 seconds to load and dispose of 145 jpgs.

Do you think 1/3 of a second is a long time to open and load a 700k file from disk?
 
Back
Top