[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

functions to get JPG and GIF dimensions

Posted on 2000-02-07
5
Medium Priority
?
245 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:Jaume Gelabert
  • 3
5 Comments
 
LVL 15

Expert Comment

by:ameba
ID: 2496734
' 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
 
LVL 32

Expert Comment

by:Erick37
ID: 2497329
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2497485
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
 
LVL 32

Accepted Solution

by:
Erick37 earned 800 total points
ID: 2497819
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
 

Author Comment

by:Jaume Gelabert
ID: 2499330
Thanks for the help!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this Micro Tutorial viewers will learn the basic shortcuts and functions of Illustrator. The viewer will learn about the paintbrush tool, anchor points, font sizing, and more.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question