Navigation

How to get the image size of a JPG, GIF and PNG image file

Answer 1

This set of functions shows how to extract the dimensions (width and height)
of JPG, GIF and PNG files. This code was done quite a while back and while it
works fine for my purposes, it may be not handle some of the newer stuff like progressive JPEGs and such. Experimentation is highly recommened.

unitImgSize;

interface

usesClasses;

procedureGetJPGSize(constsFile:string;varwWidth,wHeight:word);

procedureGetPNGSize(constsFile:string;varwWidth,wHeight:word);

procedureGetGIFSize(constsGIFFile:string;varwWidth,wHeight:word);

implementation

usesSysUtils;

functionReadMWord(f:TFileStream):word;

type

TMotorolaWord=record

casebyteof

0:(Value:word);

1:(Byte1,Byte2:byte);

end;

var

MW:TMotorolaWord;

begin

{It would probably be better to just read these two bytes in normally and

then do a small ASM routine to swap them. But we aren't talking about

reading entire files, so I doubt the performance gain would be worth the