Image dimensions

john

0
Joined
Feb 3, 2002
Messages
255
Anyone have any ideas of how to get image size (height and width), without actually loading the file into an Image object?

Im 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 Im 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 doesnt have allot of overhead but also doesnt 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
 
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.
 
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.
 
I dont know what, exactly, is going on under the hood with the Image.FromStream method. If you say it performs better, Ill 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 dont 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.
 
Back
Top