Mega Code Archive

 
Categories / Delphi / Files
 

Get width and height of a PNG image file

Title: Get width and height of a PNG image file function ReadMWord(f: TFileStream): Word; type TMotorolaWord = record case Byte of 0: (Value: Word); 1: (Byte1, Byte2: Byte); end; var MW: TMotorolaWord; begin f.read(MW.Byte2, SizeOf(Byte)); f.read(MW.Byte1, SizeOf(Byte)); Result := MW.Value; end; procedure GetPNGSize(const sFile: string; var wWidth, wHeight: Word); type TPNGSig = array[0..7] of Byte; const ValidSig: TPNGSig = (137,80,78,71,13,10,26,10); var Sig: TPNGSig; f: tFileStream; x: integer; begin FillChar(Sig, SizeOf(Sig), #0); f := TFileStream.Create(sFile, fmOpenRead); try f.read(Sig[0], SizeOf(Sig)); for x := Low(Sig) to High(Sig) do if Sig[x] ValidSig[x] then Exit; f.Seek(18, 0); wWidth := ReadMWord(f); f.Seek(22, 0); wHeight := ReadMWord(f); finally f.Free; end; end; Example usage: var width,height:word; begin GetPNGSize(filename,width, height); end;