Image.FromFile vs. New Bitmap to measure an image
So I’m currently finishing up an improved FileSyncronization routine for DotNetNuke. As in DotNetNuke, information about all files on the file system is stored in the database (not files, only info about them!) the process works in a way that the application retrieves all files from the database in a DataTable and then iterates through files and directories on the file system and compares each file to the records in the data table. Depending on the file in the data table it determines the action to be executed with the file – Create, Update, No Action and later on in the process executes the appropriate action based on the mentioned mark.
Now, I’m doing my tests on 2000+ files in one portal, with simultaneous running of both the scheduled job and the manual synchronization run, which in total runs the synchronization routines for some 4000 times in one test sequence (2000 are synced with the scheduled job and again 2000 manually)
I’ve gotten it almost fast enough for our needs, it does both the manual run and scheduled job run each in some 45 seconds (previously it was some 5 minutes), but greedy as only a human being can be… I wanted it faster! J
So I’ve fired up the profiler and went digging…it turned up that there were 2 hotspots on which I can influence:
- DataTable.Select – I will leave this for some other post
- Image.FromFile
So now we get to the point….
As in DotNetNuke along with the file info also the routine checks and saves the info on a potential images (if the file is an image, its height and width are saved along with it’s info) it uses the following code piece to do it:
Dim imgImage As System.Drawing.Image
Try
imgImage = imgImage.FromFile(sourceFilePath)
With imgImage
imageHeight = .Height
imageWidth = .Width
.Dispose()
End With
Catch
contentType = “application/octet-stream”
End Try
When profiled the application I got the following results (on 3 test runs) for the bolded function above:
|
Calls |
Execution time (ms) |
|
3119 |
26317 |
|
3119 |
21834 |
|
3119 |
24177 |
|
Avg. |
24109 |
So I went looking if I could find an alternative for the same functionality, and discovered it in the bitmap:
Dim imgBitmap As Bitmap
Try
imgBitmap = New Bitmap(sourceFilePath, False)
With imgBitmap
imageHeight = .Height
imageWidth = .Width
.Dispose()
End With
Catch
contentType = “application/octet-stream”
End Try
This one worked fine (it gave the same dimensions of the picture, it didn’t brake and such), and gave me the following info when being profiled:
|
Calls |
Execution time (ms) |
|
3119 |
17861 |
|
3119 |
18923 |
|
3119 |
16457 |
|
Avg. |
17747 |
So as you can see, on average the bitmap snip. has shown itself to be some +- 30% faster than the Image.FromFile in this scenario!

Leave a Reply