Hi,
I have been trying for days to find a way to get the image width of .png files which reside on our server. I am trying to read the first 24 bytes of the file and parse out the width from bytes 17-20. I have found several routines on the web but have not been successful. Strangely enough, it seems I am getting the height from bytes 21-24 decoded from hex to decimal just fine. I have verified the file contents using a hex viewer and the file is good. Here is the main portion of the routine:
Function ReadPNG(fichero)
Dim fso, ts, s, HW, nbytes
HW = Array("0", "0")
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(Server.MapPath("\forums\attachments/" & fichero), 1)
s = Right(ts.Read(24), 8)
HW(0) = HexToDec(HexAt(s,3) & HexAt(s,4))
HW(1) = HexToDec(HexAt(s,7) & HexAt(s,8))
ts.Close
ReadPNG = HW
End Function
Function HexAt(s, n)
HexAt = Hex(AscAt(s, n))
End Function
Function HexToDec(ByVal HexVal)
Dim i, num, part
num = 0
For I = 1 to Len(HexVal)
part = Mid(StrReverse(UCase(HexVal)), I, 1)
If IsNumeric(part) Then
num = num + (CInt(part) * 16 ^ (I - 1) )
Else
num = num + ( (Asc(part) - 55) * 16^(I - 1) )
End If
Next
HexToDec = num
End Function
As an example, my file has hex "00 00 01 80" in the width bytes (decimal 384) and hex "00 00 01 32" in the heigth bytes (decimal 306)
I am getting the heigth 306 but thee width is returning "0011" (decimal 17).
I am totally stummped! I do not have to use this routine either.
Thanks, Jim