functions to get JPG and GIF dimensions

I need functions to get the dimensions (width and height) of GIF and JPG files.
Something like this:

  GetJPGDimensions(sFile as string, lWidth as long, lHeight as long)

  GetGIFDimensions(sFile as string, lWidth as long, lHeight as long)

TIA
Jaume GelabertAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amebaCommented:
' I didn't test this, but I hope it works
   Dim pic as StdPicture
   Set pic = LoadPicture(sFile)
   lWidth = pic.Width
   lHeight = pic.Height
   Set pic = Nothing
0
Erick37Commented:
The HxW of a pic object are in HiMetric units.
lWidth = ScaleX(pic.Width, vbHimetric, vbPixels)

You can also read the dimensions from the file, without loading the whole picture first.
0
Erick37Commented:
Reading GIF dinensions from file:

Option Explicit

Private Type GIFHEADER
    ID(0 To 5) As Byte
    Width As Integer
    Height As Integer
End Type

Private Function GetGifDim(sFile As String, w As Integer, h As Integer) As Boolean
    Dim ff As Integer
    Dim gh As GIFHEADER
    Dim sID As String
    ff = FreeFile
    On Error GoTo GIFERR
    Open sFile For Random As #ff
    Get #ff, , gh
    Close #ff
    sID = StrConv(gh.ID, vbUnicode)
    sID = UCase(sID)
    If (sID <> "GIF89A") And (sID <> "GIF87A") Then
        GetGifDim = False
        Exit Function
    End If
    w = gh.Width
    h = gh.Height
    GetGifDim = True
    Exit Function
GIFERR:
    MsgBox Err.Description
    GetGifDim = False
End Function

Private Sub Command1_Click()
    'Usage
    Dim Height As Integer
    Dim Width As Integer
    On Error GoTo TESTERR
    With CommonDialog1
    .CancelError = True
    .Filter = "GIF files |*.gif"
    .ShowOpen
    Debug.Print GetGifDim(.filename, Width, Height)
    Debug.Print "W: "; Width; " H: "; Height
    End With
    Exit Sub
TESTERR:
End Sub
0
Erick37Commented:
And now for JPEG:

'JPEG file format info:
'http://wotsit.org/cgi-bin/download.cgi?jpeg

Option Explicit

Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
   pDest As Any, pSource As Any, ByVal dwLength As Long)

Private Function GetJpegDim(sFile As String, w As Integer, h As Integer) As Boolean
    Dim ff As Integer
    Dim cMarker As Byte
    Dim cChar As Byte
    Dim cLen As Integer
    Dim cData(0 To 3) As Byte
    Dim sID As String
    Dim nWidth As Integer
    Dim nHeight As Integer
    ff = FreeFile
    On Error GoTo JPGERR
    Open sFile For Binary As #ff
    Do While Not EOF(ff)
        Get #ff, , cMarker
        If cMarker = &HFF Then
            Get #ff, , cChar
            Select Case cChar
            Case &HE0 'Jpeg ID marker
                Get #ff, , cLen 'Dummy read
                Get #ff, , cData(0)
                Get #ff, , cData(1)
                Get #ff, , cData(2)
                Get #ff, , cData(3)
                sID = StrConv(cData, vbUnicode)
                sID = UCase(sID)
                If sID <> "JFIF" Then
                    GetJpegDim = False
                    Exit Function
                End If
            Case &HC0 'Image info
                Get #ff, , cLen 'Dummy read
                Get #ff, , cChar 'Dummy read
                Get #ff, , cData(1) 'Height
                Get #ff, , cData(0)
                Get #ff, , cData(3) 'Width
                Get #ff, , cData(2)
                Exit Do
            End Select
        End If
    Loop
    Close #ff
    Call MoveMemory(nWidth, ByVal VarPtr(cData(0)), 2)
    w = nWidth
    Call MoveMemory(nHeight, ByVal VarPtr(cData(2)), 2)
    h = nHeight
    GetJpegDim = True
    Exit Function
JPGERR:
    MsgBox Err.Description
    GetJpegDim = False
End Function

Private Sub Command1_Click()
    'Usage
    Dim Height As Integer
    Dim Width As Integer
    On Error GoTo TESTERR
    With CommonDialog1
    .CancelError = True
    .Filter = "JPEG files |*.jpg;*.jpeg"
    .ShowOpen
    Debug.Print GetJpegDim(.filename, Width, Height)
    Debug.Print "W: "; Width; " H: "; Height
    End With
    Exit Sub
TESTERR:
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jaume GelabertAuthor Commented:
Thanks for the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Graphics Software

From novice to tech pro — start learning today.