I want to know width and height of an image file before opening that file.
So, how to do that?
EDIT: This refers to jpg, bmp, png and gif types of image files.
I want to know width and height of an image file before opening that file.
So, how to do that?
EDIT: This refers to jpg, bmp, png and gif types of image files.
If anyone yet interested in retrieving TIFF image dimensions without loading the graphic, there is a proven method that works perfectly for me in all environments. I also found another solution for that, but it returned wrong values from Illustrator-generated TIFFs. But there is a fantastic graphic library, called GraphicEx by Mike Lischke (TVirtualStringTree's very talented developer). There are implementations of many popular image formats and all of them descend from the base class TGraphicExGraphic, that implements ReadImageProperties virtual method. It is stream-based and only reads the fileheader in all implementations. So it is lightning-fast... :-)
So, here is a sample code, that retrieves a TIFF's dimensions (the method is the same for all graphic implementation, PNG,PCD,TGA,GIF,PCX,etc):
That's all... :-) And this is the same for all the graphic implementations in the unit.
Look into exiftool.exe. It's free. It's the standard fro this kind of thing, but you'd have to shell out.
As a complement to Rafael's answer, I believe that this much shorter procedure can detect BMP dimensions:
You can try This page. I have not tested it, but it seems pretty reasonable that it will work.
Also, different file types have different ways of getting the width and height. Try to be more specific on your question.
One of the page anwser:
And for BMP (also found at the page I mentioned):
If by 'image file' you mean those raster image files recognised by the VCL's graphics system, and by 'before opening' you mean 'before the user is likely to notice that the file is opened', then you can do this very easily:
Of course, the file is opened, and this requires a lot of memory if the image is big. However, if you need to obtain metatada (like dimensions) without loading the file, I believe you need a more 'complicated' solution.