Is there a cheap way to get the dimensions of an image (jpg, png, ...)? Preferably, I would like to achieve this using only the standard class library (because of hosting restrictions). I know that it should be relatively easy to read the image header and parse it myself, but it seems that something like this should be already there. Also, I’ve verified that the following piece of code reads the entire image (which I don’t want):

It would help if you were a bit more specific in the question proper. The tags have told me .net and c#, and you want standard library, but what are these hosting restrictions you mentions?
–
wnoiseSep 21 '08 at 20:29

Hopefully the code is fairly obvious. To add a new file format you add it to imageFormatDecoders with the key being an array of the "magic bits" which appear at the begining of every file of the given format and the value being a function which extracts the size from the stream. Most formats are simple enough, the only real stinker is jpeg.

Agreed, JPEG sucks. Btw - a note for the people who want to use this code in the future: this is indeed untested. I've gone through it with a fine comb, and here's what I found: BMP format has another (ancient) header variation where dimensions are 16-bit; plus height can be negative (drop the sign then). As for JPEG - 0xC0 isn't the only header. Basically all of 0xC0 to 0xCF except 0xC4 and 0xCC are valid headers (you can easily get them in interlaced JPGs). And, to make things more fun, height can be 0 and specified later in a 0xDC block. See w3.org/Graphics/JPEG/itu-t81.pdf
–
Vilx-Feb 22 '11 at 12:36

@Vilx-, can you share the more complete code?
–
Pedro77Aug 11 '14 at 16:48

Tweaked the DecodeJfif method above to expand the original (marker == 0xC0) check to accept 0xC1 and 0xC2 as well. These other start-of-frame headers SOF1 and SOF2 encode width/height in the same byte positions. SOF2 is fairly common.
–
Ryan BartonNov 13 '14 at 20:03

I was looking for something similar a few months earlier. I wanted to read the type, version, height and width of a GIF image but couldn’t find anything useful online.

Fortunately in case of GIF, all the required information was in the first 10 bytes:

Type: Bytes 0-2
Version: Bytes 3-5
Height: Bytes 6-7
Width: Bytes 8-9

PNG are slightly more complex (width and height are 4-bytes each):

Width: Bytes 16-19
Height: Bytes 20-23

As mentioned above, wotsit is a good site for detailed specs on image and data formats though the PNG specs at pnglib are much more detailed. However, I think the Wikipedia entry on PNG and GIF formats is the best place to start.

Here’s my original code for checking GIFs, I have also slapped together something for PNGs:

Based on the answers so far and some additional searching, it seems that in the .NET 2 class library there is no functionality for it. So I decided to write my own. Here is a very rough version of it. At the moment, I needed it only for JPG’s. So it completes the answer posted by Abbas.

There is no error checking or any other verification, but I currently need it for a limited task, and it can be eventually easily added. I tested it on some number of images, and it usually does not read more that 6K from an image. I guess it depends on the amount of the EXIF data.

Yes, you can absolutely do this and the code depends on the file format. I work for an imaging vendor (Atalasoft), and our product provides a GetImageInfo() for every codec that does the minimum to find out dimensions and some other easy to get data.

If you want to roll your own, I suggest starting with wotsit.org, which has detailed specs for pretty much all image formats and you will see how to identify the file and also where information in it can be found.

If you are comfortable working with C, then the free jpeglib can be used to get this information too. I would bet that you can do this with .NET libraries, but I don't know how.

It's going to depend on the file format. Usually they will state it up in the early bytes of the file. And, usually, a good image-reading implementation will take that into account. I can't point you to one for .NET though.