Leaders John Posted August 23, 2007 Leaders Posted August 23, 2007 Anyone have any ideas of how to get image size (height and width), without actually loading the file into an Image object? I'm working on a small utility that needs to get some stats about the image files in a given directory and all subdirectories. I have a perfectly working and valid solution but I'm looking to speed it up at this point. There could be millions of files and each file could be up to 6mb in size. The costliest part of the application is where it loads the images into an Image object just to get the dimensions. Everything else I can get from a FileInfo object which doesn't have allot of overhead but also doesn't have anything that will give me the dimensions. Any pointers in the right direction, even if I have to do it as a dll outside of .NET, would be appreciated. Thanks, John Quote "These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
Leaders snarfblam Posted August 24, 2007 Leaders Posted August 24, 2007 This seems like the simplest solution to me: If you do a google search on a given image format you can easily discover where within the file the image dimensions can be found. You could then open an image with a stream and use a BinaryReader to read the image size. You would have to write code to handle each image format, though. Quote [sIGPIC]e[/sIGPIC]
Leaders John Posted August 28, 2007 Author Leaders Posted August 28, 2007 Image.FromStream did the trick. There is an overload that allows you to skip the verification process which is apparently the main source of the bottleneck. Especially with jpeg files. Quote "These Patriot playoff wins are like Ray Charles songs, Nantucket sunsets, and hot fudge sundaes. Each one is better than the last." - Dan Shaughnessy
Leaders snarfblam Posted August 29, 2007 Leaders Posted August 29, 2007 I don't know what, exactly, is going on under the hood with the Image.FromStream method. If you say it performs better, I'll take your word for it, but there may be considerations other than performance. My particular concern would be that unnecessary large memory allocations would be made (they might not be, I don't know), which can wreak havoc on the GC. You might want to see if you can get a better idea of what Image.FromStream does (check documentation or look under the hood with reflector). Just my two cents. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.