Help! - jpg file dimensions 
Author Message
 Help! - jpg file dimensions

Need a little pointer...

Is there an easy way to get the image dimensions from a jpg file?
IE: X-pixels by Y-pixels

Note: I'm not good with APIs



Sun, 12 Oct 2003 23:54:33 GMT  
 Help! - jpg file dimensions
Forget it guys, I figured it out.

"Use the ImgAdmin control."


Quote:
> Need a little pointer...

> Is there an easy way to get the image dimensions from a jpg file?
> IE: X-pixels by Y-pixels

> Note: I'm not good with APIs



Mon, 13 Oct 2003 00:27:55 GMT  
 Help! - jpg file dimensions
Not so fast there Jim . . . I don't have an ImgAdmin control available on my
system. To me, that would mean it is a control that was added to your system
by some program that you installed, so your solution is not "general". A
non-API kludge that is very simple to do is to place an ImageBox on your
Form, set its Visible property to False and (most importantly) make sure its
Stretch property is set to False. You can then load the picture into the
ImageBox using this

     Image1.Picture = LoadPicture(YourPictureFileNameAndPath)

The pixel size is calculated as follows

       JPGwidth = Image1.Width / Screen.TwipsPerPixelX
       JPGheight = Image1.Height / Screen.TwipsPerPixelY

Rick


Quote:
> Forget it guys, I figured it out.

> "Use the ImgAdmin control."



> > Need a little pointer...

> > Is there an easy way to get the image dimensions from a jpg file?
> > IE: X-pixels by Y-pixels

> > Note: I'm not good with APIs



Mon, 13 Oct 2003 02:39:04 GMT  
 Help! - jpg file dimensions
You could load it into a hidden picture box or an image control and then get
the width and height of the control.
Eric C.


Tue, 14 Oct 2003 10:17:46 GMT  
 Help! - jpg file dimensions
On Wed, 25 Apr 2001 10:54:33 -0500, "Jim Millar"

Quote:
>Need a little pointer...

>Is there an easy way to get the image dimensions from a jpg file?
>IE: X-pixels by Y-pixels

>Note: I'm not good with APIs

Here is some code I found a few years ago, but I can't remember who
the original author was. I've noticed that my posts sometimes show up
garbled, so if that happens to this one, just email me and I'll zip
the source and send it that way.

Option Explicit
Private Type ImageInfo
    Filetype As Byte
    Width As Long
    Height As Long
    Depth As Byte
End Type
Const PNG As Byte = 1
Const GIF As Byte = 2
Const JPG As Byte = 3
Const BMP As Byte = 4
Const ERROR As Byte = 255

Private Function GetImageInfo(SPath As String) As ImageInfo
    On Error GoTo Handle
    Dim IFileNum As Integer
    Dim BTemp(2) As Byte
    Dim UTemp As ImageInfo
    Dim LPointer As Long
    GetImageInfo.Filetype = ERROR
    IFileNum = FreeFile
    Open SPath For Binary As IFileNum
    Get #IFileNum, 1, BTemp()
    If BTemp(0) = 137 And BTemp(1) = 80 And BTemp(2) = 78 Then
        UTemp = GetPng(IFileNum)
        If UTemp.Filetype <> ERROR Then GetImageInfo = UTemp
    End If
    If BTemp(0) = 71 And BTemp(1) = 73 And BTemp(2) = 70 Then
        UTemp = GetGif(IFileNum)
        If UTemp.Filetype <> ERROR Then GetImageInfo = UTemp
    End If
    If BTemp(0) = 66 And BTemp(1) = 77 Then
        UTemp = GetBmp(IFileNum)
        If UTemp.Filetype <> ERROR Then GetImageInfo = UTemp
    End If
    If GetImageInfo.Filetype = ERROR Then
        LPointer = CheckJpg(IFileNum)
        If LPointer <> -1 Then
            UTemp = GetJpg(IFileNum, LPointer)
            If UTemp.Filetype <> ERROR Then GetImageInfo = UTemp
        End If
    End If
    Close IFileNum
    Exit Function
Handle:
    GetImageInfo.Filetype = ERROR
    Close IFileNum
End Function

Private Function GetPng(IFileNum As Integer) As ImageInfo
    On Error GoTo Handle
    Dim Msb As Byte
    Dim Lsb As Byte
    GetPng.Filetype = PNG
    Get #IFileNum, 19, Msb
    Get #IFileNum, 20, Lsb
    GetPng.Width = Mult(Lsb, Msb)
    Get #IFileNum, 23, Msb
    Get #IFileNum, 24, Lsb
    GetPng.Height = Mult(Lsb, Msb)
    Get #IFileNum, 25, Msb
    Get #IFileNum, 26, Lsb
    Select Case Lsb
    Case 0
        GetPng.Depth = Msb
    Case 2
        GetPng.Depth = Msb * 3
    Case 3
        GetPng.Depth = 8
    Case 4
        GetPng.Depth = Msb * 2
    Case 6
        GetPng.Depth = Msb * 4
    Case Else
        GetPng.Filetype = ERROR
    End Select
    Exit Function
Handle:
    GetPng.Filetype = ERROR
End Function

Private Function GetGif(IFileNum As Integer) As ImageInfo
    On Error GoTo Handle
    Dim Msb As Byte
    Dim Lsb As Byte
    GetGif.Filetype = GIF
    Get #IFileNum, 7, Lsb
    Get #IFileNum, 8, Msb
    GetGif.Width = Mult(Lsb, Msb)
    Get #IFileNum, 9, Lsb
    Get #IFileNum, 10, Msb
    GetGif.Height = Mult(Lsb, Msb)
    Get #IFileNum, 11, Lsb
    GetGif.Depth = (Lsb And 7) + 1
    Exit Function
Handle:
    GetGif.Filetype = ERROR
End Function

Private Function GetBmp(IFileNum As Integer) As ImageInfo
    On Error GoTo Handle
    Dim Msb As Byte
    Dim Lsb As Byte
    GetBmp.Filetype = BMP
    Get #IFileNum, 19, Lsb
    Get #IFileNum, 20, Msb
    GetBmp.Width = Mult(Lsb, Msb)
    Get #IFileNum, 23, Lsb
    Get #IFileNum, 24, Msb
    GetBmp.Height = Mult(Lsb, Msb)
    Get #IFileNum, 29, Lsb
    GetBmp.Depth = Lsb
    Exit Function
Handle:
    GetBmp.Filetype = ERROR
End Function

Private Function CheckJpg(IFileNum As Integer) As Long
    On Error GoTo Handle
    Dim FoundFlag As Byte
    Dim BBuf(3) As Byte
    Dim LPos As Long
    Dim Length As Long
    Length = LOF(IFileNum)
    Do While LPos < Length And FoundFlag = 0
        LPos = LPos + 1
        Get #IFileNum, LPos, BBuf()
        If BBuf(0) = 255 And BBuf(1) = 216 And BBuf(2) = 255 Then
            FoundFlag = 1
            CheckJpg = LPos
        End If
    Loop
    If FoundFlag = 0 Then
        CheckJpg = -1
    End If
    Exit Function
Handle:
    CheckJpg = -1
End Function

Private Function GetJpg(IFileNum As Integer, LPos As Long) As
ImageInfo
    On Error GoTo Handle
    Dim Length As Long
    Dim Byt As Byte
    Dim Lsb As Byte
    Dim Msb As Byte
    GetJpg.Filetype = JPG
    Length = LOF(IFileNum)
    LPos = LPos + 2
Back:
    If LPos > Length Then GoTo Handle
    Get #IFileNum, LPos, Byt
    If Byt = 255 Then
        LPos = LPos + 1
        GoTo Back
    End If
    If Byt < 192 Or Byt > 195 Then
        Get #IFileNum, LPos + 1, Msb
        Get #IFileNum, LPos + 2, Lsb
        LPos = LPos + Mult(Lsb, Msb) + 1
        GoTo Back
    End If
    Get #IFileNum, LPos + 4, Msb
    Get #IFileNum, LPos + 5, Lsb
    GetJpg.Height = Mult(Lsb, Msb)
    Get #IFileNum, LPos + 6, Msb
    Get #IFileNum, LPos + 7, Lsb
    GetJpg.Width = Mult(Lsb, Msb)
    Get #IFileNum, LPos + 8, Lsb
    GetJpg.Depth = Lsb * 8
    Exit Function
Handle:
    GetJpg.Filetype = ERROR
End Function

Private Function Mult(Lsb As Byte, Msb As Byte) As Long
    Mult = CLng(Lsb + (Msb * 256))
End Function

        HTH,
        J
        Jeremiah D. Seitz
        Porch karaoke king and the guy who runs with 8< scissors >8
        Omega Techware
        http://omegatechware.hypermart.net



Tue, 14 Oct 2003 11:12:12 GMT  
 Help! - jpg file dimensions

Quote:
> Forget it guys, I figured it out.
> "Use the ImgAdmin control."

I don't know what the ImgAdmin control is, but you could have just loaded
the JPG into a auto-resized Picture Box and checked its width/height
properties.

Regards, Otser.



Mon, 13 Oct 2003 12:45:42 GMT  
 Help! - jpg file dimensions

Quote:

>Need a little pointer...

>Is there an easy way to get the image dimensions from a jpg file?
>IE: X-pixels by Y-pixels

Check out the (ancient) VB code from JPEG viewer. The main sub is
Info_JPG, in GV.FRM. You won't be able to use the 16-bit DLL's, but that
doesn't really matter, does it?

    <http://www.programmersheaven.com/search/download.asp?FileID=119>

--
        Bart.



Tue, 14 Oct 2003 19:22:22 GMT  
 Help! - jpg file dimensions
Thanks Guys.

The hidden image/picturebox was how I was doing it originally.
But it seemed to be very 'lowtech'
I am at the beginner to intermediate level, I thought there might be a
'method'
that I didn't know about.
Seems the Occums razor thing is true...

The simplest answer is usually the correct answer.

Thanks
Jim



Tue, 14 Oct 2003 21:50:59 GMT  
 Help! - jpg file dimensions


Quote:
>The hidden image/picturebox was how I was doing it originally.
>But it seemed to be very 'lowtech'
>I am at the beginner to intermediate level, I thought there might be a
>'method'
>that I didn't know about.

Higher tech is to use the API Function LoadPicture and get the
dimensions from there without using an Image control/PictureBox.

Even higher tech is to use the free Intel JPEG library which also
enables one to convert bmp's to JPEG.

--
Richard Mason



Wed, 15 Oct 2003 03:41:08 GMT  
 Help! - jpg file dimensions

Quote:

>The hidden image/picturebox was how I was doing it originally.
>But it seemed to be very 'lowtech'

It is.

Quote:
>I am at the beginner to intermediate level, I thought there might be a
>'method' that I didn't know about.

So you're back to the image box? Very bad. All you want is the
dimensions, and you're decompression the whole picture for it. Not only
is that slow, just try getting the dimensions of a whole subtree for
example; but decompressed images are really big, often well over a even
megabyte, so you're waisting memory, too.

The VB code to extract the chunk with the info you want is much more
efficient, but in memory as in speed. It doesn't even start to look at
the image itself.

--
        Bart.



Wed, 15 Oct 2003 18:55:09 GMT  
 Help! - jpg file dimensions
Aha!
Sounds like you really know what your doing.
But, for the ball of wax can you post how to:
"extract the chunk with the info"

Jim


Quote:

> >The hidden image/picturebox was how I was doing it originally.
> >But it seemed to be very 'lowtech'

> It is.

> >I am at the beginner to intermediate level, I thought there might be a
> >'method' that I didn't know about.

> So you're back to the image box? Very bad. All you want is the
> dimensions, and you're decompression the whole picture for it. Not only
> is that slow, just try getting the dimensions of a whole subtree for
> example; but decompressed images are really big, often well over a even
> megabyte, so you're waisting memory, too.

> The VB code to extract the chunk with the info you want is much more
> efficient, but in memory as in speed. It doesn't even start to look at
> the image itself.

> --
> Bart.



Fri, 17 Oct 2003 20:30:06 GMT  
 Help! - jpg file dimensions
Thanks,

The higher tech solution looks pretty good.
(if I can figure out how to work it...)

Jim


Quote:


> >The hidden image/picturebox was how I was doing it originally.
> >But it seemed to be very 'lowtech'
> >I am at the beginner to intermediate level, I thought there might be a
> >'method'
> >that I didn't know about.

> Higher tech is to use the API Function LoadPicture and get the
> dimensions from there without using an Image control/PictureBox.

> Even higher tech is to use the free Intel JPEG library which also
> enables one to convert bmp's to JPEG.

> --
> Richard Mason



Fri, 17 Oct 2003 20:43:26 GMT  
 Help! - jpg file dimensions

Quote:

>Aha!
>Sounds like you really know what your doing.
>But, for the ball of wax can you post how to:
>"extract the chunk with the info"

Ah, a challenge. The code in the ZIP file I mentioned, is enough to get
it working, except that it dumps its results into a listbox instead of
something more low-level.

So here is my variation. Save as a module. It works from VB3 on.

    Option Explicit

    Const M_SOF0 = &HC0, M_SOF1 = &HC1, M_SOF2 = &HC2, M_SOF3 = &HC3
    Const M_SOF5 = &HC5, M_SOF6 = &HC6, M_SOF7 = &HC7, M_SOF9 = &HC9
    Const M_SOF10 = &HCA, M_SOF11 = &HCB, M_SOF13 = &HCD
    Const M_SOF14 = &HCE, M_SOF15 = &HCF, M_SOI = &HD8
    Const M_EOI = &HD9, M_SOS = &HDA, M_COM = &HFE

    Type JPEGinfo
        width As Integer
        height As Integer
        BitsPerPixel As Integer
        ColorPlanes As Integer
        JPEGTYPE As Integer
    End Type

    Function GetC% (ByVal FH%)
        Dim BT As String * 1
        Get #FH, , BT
        GetC = Asc(BT)
    End Function

    Function GetInt& (ByVal FH%)
        Dim C1%, C2%, N&
        C1 = GetC(FH)
        C2 = GetC(FH)
        GetInt = CLng(C1) * 256 + C2
    End Function

    Function Info_JPEG (ByVal File$, JPEGinfo As JPEGinfo) As Integer
        'returns True on success, False otherwise
        Dim FH%, Length&, C1%, C2%, Marker%
        FH = FreeFile
        Open File$ For Binary As FH
        C1 = GetC(FH): C2 = GetC(FH)
        If C1 = &HFF And C2 = M_SOI Then
           Do
               C1 = GetC(FH)  '&HFF
               Marker = GetC(FH)
               Select Case Marker
               Case M_SOF0 to M_SOF3, M_SOF5 to M_SOF15
                 Length = GetInt(FH)
                 JPEGinfo.BitsPerPixel = GetC(FH)
                 JPEGinfo.height = GetInt(FH)
                 JPEGinfo.width = GetInt(FH)
                 JPEGinfo.ColorPlanes = GetC(FH)
                 JPEGinfo.JPEGTYPE = Marker
                 Info_JPEG = True
                 Exit Do
               Case M_SOS, M_SOI, M_EOI, -1
                 Exit Do
               Case Else
                 ' skip field
                 Length = GetInt(FH)
                 If Length < 2 Then Exit Do  'error
                 Seek #FH, Seek(FH) + Length - 2
               End Select
            Loop
        End If
        Close FH
    End Function

Call as (in a form, with its autoredraw set to true):

    dim success%
    cmdialog1.Action = 1
    success = Info_JPEG(cmdialog1.Filename, JPEGinfo)
    Cls
    If success Then
        Print cmdialog1.Filetitle
        Print JPEGinfo.width; " x "; JPEGinfo.height
        Print JPEGinfo.ColorPlanes;" x "; JPEGinfo.BitsPerPixel;" bits"
        Print "Image type: " & Hex$(JPEGinfo.JPEGTYPE)
    End If

--
        Bart.



Sat, 18 Oct 2003 22:23:47 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Using VBA in A2k to get dimensions of a jpg file

2. jpg file dimension retrieval

3. Dimensions of GIF and JPG from files

4. Jpg File Dimensions / Html

5. jpg file dimension retrieval

6. Dimensions of GIF and JPG from files

7. .bmp, .gif, .jpg dimensions

8. ? How to get dimensions of gif or jpg

9. Dimensions on gif / jpg

10. convert graphic type formats EPS to JPG, PCX to JPG, TIFF to JPG, etc

11. single dimension array into multiple dimension array

12. Dimensioning of single dimension arrays within a class

 

 
Powered by phpBB® Forum Software