System.Drawing.Image Performance
As I mentioned earlier, I wrote an application called JPEG Hammer for manipulating and viewing EXIF data in digital photos. As a .NET Application the single biggest performance bottleneck has been:
Image photo = Image.FromFile(fileName, true);
For a 6.1 megapixel (3008 x 2000) 1.47 MB JPEG it took on the order of 631 milliseconds to load. Multiply this by 50 or so photos and you are talking about 32 seconds to fully load these pictures. Then add the time necessary to draw the UI for the app, update thumbnails etc. On a 1.8 ghz Pentium 4 I'd expect more... and lets just say it's no where near as fast as the XP Shell (or other non .NET phot apps), so what gives?
Well, after much Googling (with no success) I tried https://support.microsoft.com/ and I came across this gem (Microsoft KB 831419):
FIX: Slow performance when you call System.Drawing.Image.FromStream to load a bitmap image
So, curious as I am, I get my hands on this hotfix which updates:
- System.Windows.Forms.dll
- System.Design.dll
- System.Drawing.dll
Specifically, this update adds a new method to System.Drawing.Imaging:
System.Drawing.Image.FromStream(Stream stream, bool useICM, bool validateImageData)
This is essentially a new signature for an existing method:
System.Drawing.Image.FromStream(Stream stream, bool useICM)
As you can see, validateImageData is a new parameter. Setting it to true is the default behavior that we have today (essentially the same as calling FromStream(Stream stream, bool useICM)).
So I made a change to my application. Before my code looked like this:
using (Image photo = Image.FromFile(this.fileInfo.FullName, true))
{
//do stuff
}
So I changed it to:
using (FileStream fs = new FileStream(this.fileInfo.FullName, FileMode.Open, FileAccess.ReadWrite))
{
using (Image photo = Image.FromStream(fs, true, false))
{
// do stuff
}
}
And here are the results. The average time it took to process 37 JPEGs taken from the same camera and roughly the same size went from 631ms to 6.76ms. So, this new method is 93x faster. Holy Cow!!! I thought that maybe something was wrong, so I re-ran the tests many times, and always got the same result. Simply mind boggling. This probably brings the .NET implementation to the same speed as writing native C++ code against GDI+.
The good news is that the article mentions that this fix will get rolled into the next Service Pack. However, you'll need to modify your code as I did to take advantage of this.
Update: you can download this fix in SP1 of the .NET Framework 1.1:
Comments
Anonymous
March 28, 2004
The comment has been removedAnonymous
March 28, 2004
This is another proof that MS should release a .Net 1.2 before whidbey !
One of the multiple patches which are so necessary.Anonymous
March 28, 2004
Do you think that there will be a .Net 1.1 SP this year? Does anyone actually know?Anonymous
March 28, 2004
This patch would need to be installed on each end user's machine. I'm not sure what the mechanism is like for rolling hotfixes into the MSI installer for instance, but I'm sure it's possible or else these hotfixes would be useless.
As for future releases of .NET I have no idea...Anonymous
March 28, 2004
Can you publish or send me the patch ?Anonymous
March 28, 2004
I believe you need to "contact PSS" per the KB article to get the patch. I happen to get it internally, and I'm not sure I can distribute it. I wouldn't even know who to ask really (remember, I work on Mac stuff ;-)).
The KB article states:
"To resolve this problem immediately, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services phone numbers and information about support costs, visit the following Microsoft Web site:
http://support.microsoft.com/default.aspx?scid=fh;[LN];CNTACTMS"
I would just try and send them an e-mail asking for it.
Try this link:
https://webresponse.one.microsoft.com/oas/public/assistedintro.aspxAnonymous
March 28, 2004
The comment has been removedAnonymous
March 29, 2004
That's correct.Anonymous
March 29, 2004
Notes! It was 1am, so I didn't read about the hotfix too much. Guess we'll have to wait and see if they come out with something better in Whidbey,Anonymous
March 29, 2004
[.NET]System.Drawing.Image PerformanceAnonymous
March 31, 2004
The comment has been removedAnonymous
March 31, 2004
Also, Omar, if you would be so kind as to possibly test my method within your framework to see if it offers the same speed I would be appreciative. While the code-paths I'm running should demonstrate a truncated version of what even the updated methods are doing, I'm not 100% certain they aren't also adding additional performance benefits. Thanks.Anonymous
June 26, 2004
The comment has been removedAnonymous
May 29, 2009
PingBack from http://paidsurveyshub.info/story.php?title=omar-shahine-s-weblog-system-drawing-image-performanceAnonymous
June 01, 2009
PingBack from http://woodtvstand.info/story.php?id=5305Anonymous
June 02, 2009
PingBack from http://woodtvstand.info/story.php?id=46493