Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Aug 2001
    Location
    Spring City, Pennsylvania, USA
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JPG Object Exposes Itself? (Access 97)

    Hi All,
    I did not see this question asked before: Do any of you know a way to determine whether a linked OLE type graphic (JPG) object can be made to tell you whether it is portrait or landscape orientation? I'd like to have a picture appear on a Form in, say, one of two Bound OLE fields, (or maybe even vary the field dimensions under VBA control!) as either portrait-oriented or landscape-oriented, but I need a way to let the VBA program determine the the picture orientation (or dimensions or something) beforehand.
    Thanks! (PS: This board is a *great* source of info!) -dBETTSXX

  2. #2
    New Lounger
    Join Date
    Aug 2001
    Location
    Spring City, Pennsylvania, USA
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: JPG Object Exposes Itself? (Access 97)

    No answers were forthcoming, so I heaved a sigh and dug into the .JPG file structure. To save any of you (that might be inclined) the trouble, here is some VBA code that allows you to look into a .JPG file and determine it's orientation:

    Dim ByteStr As String
    Dim Ydim, Xdim As Integer

    ' Open the file for "input"
    Open Cathnamefilename.JPG For Input As #1
    Do While True
    ByteStr = Input(1, #1)
    ' Search for an FFh (255d) byte, followed by a C0h (192d) byte (marker for file size block).
    If Asc(CVar(ByteStr)) = 255 Then
    ByteStr = Input(1, #1)
    If Asc(CVar(ByteStr)) = 192 Then
    ' Throw away next two bytes after the FF C0 block marker.
    ByteStr = Input(2, #1)
    ' Get the next four bytes. They contain the image dimensions in pixels.
    ByteStr = Input(4, #1)
    Exit Do
    End If
    End If
    Loop
    Close #1
    ' The first two of the four pixel bytes contain the "Y" (vertical) dimension.
    Ydim = Asc(CVar(Mid(ByteStr, 1, 1))) * 256 + Asc(CVar(Mid(ByteStr, 2, 1)))
    ' The second two of the four pixel bytes contain the "X" (horizontal) dimension.
    Xdim = Asc(CVar(Mid(ByteStr, 3, 1))) * 256 + Asc(CVar(Mid(ByteStr, 4, 1)))

    ' Lastly, compare the y-dimension to the x-dimension...
    If Ydim > Xdim Then
    ' set OLE picture field (in ACESS form) to portrait orientation
    ' Picture dimensions are set in "twips", see Access Help for definition
    olePicture.Width = 2665
    olePicture.Height = 3073
    Else
    ' set OLE picture field (in ACESS form) to landscape orientation
    olePicture_1.Width = 3073
    olePicture_1.Height = 2665
    End If

    Hope you all find this useful! - DB

  3. #3
    3 Star Lounger
    Join Date
    Apr 2001
    Location
    Levin, Manawatu-Wanganui, New Zealand
    Posts
    324
    Thanks
    9
    Thanked 28 Times in 26 Posts

    Re: JPG Object Exposes Itself? (Access 97)

    Hi
    Thank you for that.
    I learned a lot from that technique & surprise!! I can apply it somewhere else.

    Thanks again
    Geof

  4. #4
    New Lounger
    Join Date
    Jan 2001
    Location
    Vermont
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: JPG Object Exposes Itself? (Access 97)

    I think the JPG file format is fairly parameterized, that is to say what's in the file is identified by a series of block headers telling you what's coming. Here's some code that will accept a couple of other kinds of size blocks, and skip over blocks it doesn't recognize. I think it's a little more robust than what's above, but I wrote it a while ago.

    Regards,
    Jordan

    Sub JPEGSize(JPEGfile As FileName, x As Integer, y As Integer)
    ' following code is a total rip-off of the PERL form # Andrew Tong, werdna@ugcs.caltech.edu February 14, 1995
    ' modified slightly by alex@ed.ac.uk, rewritten in VB by jordan@gurus.com
    x = -1: y = -1
    Dim f As Integer: f = FreeFile
    Open JPEGfile.FullPath For Binary As #f

    Dim c1 As String, c2 As String, l As String, discard As String, xh As String, xl As String, yh As String, yl As String
    Dim bc As Long
    Dim bl As Integer

    c1 = Input(1, f)
    c2 = Input(1, f)
    If Not ((Asc(c1) = &HFF) And (Asc(c2) = &HD8)) Then
    Call MsgBox("Doesn't look like a jpg", vbOKOnly, "oops")
    Else
    bc = 2: l = " "
    Do While True
    ' find next FF
    Do While (Asc(l) <> &HFF) And Not EOF(f): l = Input(1, f): bc = bc + 1: Loop
    ' skip all the FFs
    Do While (Asc(l) = &HFF) And Not EOF(f): l = Input(1, f): bc = bc + 1: Loop
    ' is it a size block?
    If Asc(l) >= &HC0 And Asc(l) <= &HC3 Then
    ' EOFs could happen here, but if they do it's because the file's messed up
    On Error GoTo done
    discard = Input(3, f)
    yh = Input(1, f): yl = Input(1, f): xh = Input(1, f): xl = Input(1, f)
    x = Asc(xh) * 256 + Asc(xl)
    y = Asc(yh) * 256 + Asc(yl)
    Exit Do
    End If
    ' it's some other kind of block; skip over it
    c1 = Input(1, f)
    c2 = Input(1, f)
    bl = Asc(c1) * 256 + Asc(c2)
    If bl > 1 Then
    discard = Input(bl - 2, f)
    bc = bc + bl
    End If
    Loop
    End If

    done:
    On Error GoTo 0
    Close #f

    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •