Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JPEG dimensions 1

Status
Not open for further replies.
I have found this piece of code, but it doesn't work. It does get the name correctly, it does open the file, it does read the bytes, but it does NOT return the values for height and width, with several JPEGs that I've tried.

Sub GetJpegDims(ByVal strFileName, ByRef lngHeight, ByRef lngWidth)
'on error resume next
Dim stmFile
set stmFile = server.createobject("ADODB.Stream")
Dim bytArr(256)
dim byt
Dim intPos
With stmFile
.Type = 1 'adTypeBinary
.Open
.LoadFromFile strFileName
.Position = 0

for intPos = 0 to 255
.position = intpos
bytArr(intPos) = ascb(.Read(1))
response.write(ascb(.Read(1)))
response.write(" ")
next
.Close
End With
Set stmFile = Nothing

For intPos = 0 To 255
If bytArr(intPos) = &HFF And bytArr(intPos + 1) >= &HC0 _
And bytArr(intPos + 1) <= &HCF Then
lngHeight = bytArr(intPos + 5) * 256 + bytArr(intPos + 6)
lngWidth = bytArr(intPos + 7) * 256 + bytArr(intPos + 8)
Exit For
End If
Next
End Sub

Dim h2, w2, fname

fname = Server.MapPath("img1.jpg")

Response.Write(fname)

Call GetJpegDims(fname,h2,w2)

Response.Write(h2+"<br>")
Response.Write(w2)


regards,
rydel n23
 
I don't know why you say it does not work. It should and it does.

The simplest way to get the dimension in pixel, though, is to use native loadpicture function. It has limitation on the pic formats and some other aspects. But common formats bmp, ico, jpg and gif are supported.
[tt]
set opic=loadpicture(server.mappath("img1.jpg"))
'height and width properties return in himetric (0.01mm)
'numeric factors are just to convert them to pixel
h2=round(opic.height/2540*96)
w2=round(opic.width/2540*96)
set opic=nothing
response.write h2 & " x " & w2 & " (HxW in unit pixel)<br />"
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top