• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2001
  • Last Modified:

get pixel height and width ( and depth?) in vb6 from an image file

How do i get the pixel height and width properties on a specific jpg file in vb6?
I dont mind a frigged way of doing it as its for a once off program  run to validate a whole lot of files.

1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
Something like this:

Private Sub Command1_Click()
    On Error GoTo cancelled:
    Dim img As StdPicture
    Dim imgWidth As Single
    Dim imgHeight As Single
    CommonDialog1.Filter = "Pictures (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif"
    Set img = LoadPicture(CommonDialog1.FileName)
    imgWidth = Round(Me.ScaleX(img.Width, vbHimetric, vbPixels))
    imgHeight = Round(Me.ScaleY(img.Height, vbHimetric, vbPixels))
    MsgBox CommonDialog1.FileName & vbCrLf & imgWidth & "x" & imgHeight
End Sub
the following will get the height and width info from a jpg.
Hope it helps some    

    Dim sFilePath As String
    Dim lFileNum As Long
    Dim lJPGStart As Long
    Dim sBuff As String
    Dim lTemp As Long
    Dim lHeight As Long
    Dim lWidth As Long

    sFilePath = "C:\test.jpg"
    ' Read the JPG file into memory
    lFileNum = FreeFile
    Open sFilePath For Binary Access Read As lFileNum
    sBuff = Space(LOF(1))
    Get #lFileNum, 1, sBuff
    Close lFileNum
    ' Look for the image info block (FF,C0)
    If Mid(sBuff, 1, 2) <> Chr(&HFF) & Chr(&HD8) Then
        MsgBox "Not a valid JPG file"
        lJPGStart = 2
                lJPGStart = lJPGStart + 1
                If Mid(sBuff, lJPGStart - 1, 1) = Chr(&HFF) And Mid(sBuff, lJPGStart, 1) <> Chr(&HFF) Then Exit Do

            Select Case Asc(Mid(sBuff, lJPGStart, 1))
                Case &HC0 To &HC3, &HC5 To &HC7, &HC9 To &HCB, &HCD To &HCF: Exit Do
            End Select
            lJPGStart = lJPGStart + ((CLng(Asc(Mid(sBuff, lJPGStart + 1, 1))) * 256) + Asc(Mid(sBuff, lJPGStart + 2)))

            If lJPGStart >= Len(sBuff) Then
                MsgBox "bad JPG file"
                Exit Do
            End If
        ' Fetch the image hight and with
        lHeight = ((CLng(Asc(Mid(sBuff, lJPGStart + 4, 1))) * 256) + Asc(Mid(sBuff, lJPGStart + 5)))
        lWidth = ((CLng(Asc(Mid(sBuff, lJPGStart + 6, 1))) * 256) + Asc(Mid(sBuff, lJPGStart + 7)))
        MsgBox "Height = " & lHeight & vbCrLf & "Width = " & lWidth
    End If
brianhardingAuthor Commented:
terrifc thanks

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now