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

Posted on 2004-11-15
Last Modified: 2012-06-21
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.

Question by:brianharding
    LVL 85

    Accepted Solution

    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
    LVL 3

    Expert Comment

    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

    Author Comment

    terrifc thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Javascript: Crash Course

    Javascript is not only emerging as the most important language to know, it's flexibility is unique. With Javascript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know.

    Suggested Solutions

    Title # Comments Views Activity
    has12 challenge 13 52
    scores100 challenge 3 67
    bigHeights  challenge 13 43
    powerN  challenge 3 34
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    A short article about problems I had with the new location API and permissions in Marshmallow
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now