views:

1110

answers:

6

I would like to find the height/width of an image on disk without opening it, if possible (for performance reasons).

The Windows properties pane for images contains information like width, height, bit depth, etc., which leads me to believe that it is storing metadata on the file somewhere. How can I access this information?

+1  A: 

The easiest way to accomplish this is, assuming the image is square is to take the file size in bytes and take the square root. This will be your width and height.

256 bytes = 16px x 16px

:-)

Or, you can try reading the image's EXIF information using this codeplex library

Chris Ballance
Big assumption that the images are square.
SteveM
I always wondered how Windows calculated this so quickly!
routeNpingme
Wow! Images might not be square. Hmmmmmm this throws a wrench in it! ;)
Rex M
For images which are not square, this method becomes infinitely more difficult.
Chris Ballance
Voted up just because it's cool. May not be right, but it's pretty neat to know.
George Stocker
In all seriousness, if you factor out compression (such as JPEG), a refinement on this method might be reasonably accurate.
Chris Ballance
This might work for (square) bitmaps, but surely not compressed formats?
Gabe Moothart
Only works if image is square, 1 byte per pixel, no alpha channel, file has no header, padding, metadata, no color maps, indexes, not compressed or encoded, ... i.e. never.
Dour High Arch
It's a joke. Jesus people. Just laugh, upvote and move on.
Rex M
A perfectly square bitmap-like joke? Or is this kind of a compressed, non-square joke?
routeNpingme
+2  A: 

There are some stackoverflow questions on how to read the EXIF information from images, such as: http://stackoverflow.com/questions/58649/how-to-get-the-exif-data-from-a-file-using-c

Zed
EXIF data is stored inside the file, so that would require opening it. Which does not meet OP's criteria - not saying OP's criteria makes any sense :)
Rex M
I think the requirement of the OP is more in the direction that he wants to skip decompressing the image and allocation memory for the bitmap data. If that was the case, accessing meta data is fine.
0xA3
IMO he means that not loading the actual image data, opening and reading metadata should be fine. Otherwise there are no ways inspect that file. The real perf bottleneck will be loading the image data, not the metadata.
huseyint
+1  A: 

Windows doesn't store (this) metadata in any special place in the filesystem; the Properties window simply reads them from the image file itself.

I don't think .NET offers any functions to read just the metadata from an image without loading the entire image. If you're dealing with only a limited number of different image formats (e.g. only JPEG, PNG and GIF), it shouldn't be too hard to read the size from the image header yourself.

If, on the other hand, you have to deal with many image formats, maybe you can have a look at the source code of the Unix file utility. It manages to detect the size of many, many different image formats, and is blazingly fast to boot.

Thomas
+1  A: 

In order to get the width and height of an image (essentially, as you put it, the metadata) you will have to open the file, parse some kind of header information and obtain what you want that way.

You would not have to read all the color/bitmap information, only the header.

This is the same way Windows is able to load icons from application files without actually executing them.

Mike J
+1  A: 

Check this question:

CMS
A: 

To read the properties displayed by Windows Explorer you can use the Microsoft Shell Controls and Automation component. The advantage of this is that you don't need any third party library (the COM object is already there) or additional code for parsing the image header and that it will work with a variety of formats.

Sample code can be found in an answer to a related question.

0xA3