Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted

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

"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
Posted
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.
[sIGPIC]e[/sIGPIC]
  • Leaders
Posted
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.
"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
Posted
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.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...