Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Read JPG Header Comment (VB6)

    I found this code in Google, used to read header info from a JPG.
    I am hoping this code will return any comments added.
    My hope is to have some kind of marker next to the image to show the user there are comments attached to the image.
    Can some-one help to explain how to use this code, do I need some kind of text control to show the info etc.

    <pre> Public Type BITMAPINFO
    Width As Long
    Height As Long
    End Type

    Public Function GetJPGInfo(ByVal FileName As String) As BITMAPINFO
    Dim bChar As Byte
    Dim a As Byte, b As Byte
    Dim c As Byte, D As Byte
    Dim E As Byte, f As Byte
    Dim i As Integer
    Dim DotPos As Integer
    Dim Header As String
    Dim blExit As Boolean
    Dim MarkerLen As Long
    Dim ImgWidth As Integer
    Dim ImgHeight As Integer
    Dim ImgSize As String
    Dim fnum As Integer
    Dim ImageInfo As BITMAPINFO
    On Error Resume Next

    fnum = FreeFile
    Open FileName For Binary As #fnum

    ImgSize = LOF(fnum) / 1024
    DotPos = InStr(ImgSize, ",")
    ImgSize = Left(ImgSize, DotPos)

    Get #fnum, , bChar
    Header = Hex$(bChar)
    Get #fnum, , bChar
    Header = Header & Hex$(bChar)
    If Header <> "FFD8" Then Exit Function

    While Not blExit
    Do Until Hex$(bChar) = "FF"
    Get #fnum, , bChar
    Loop

    Get #fnum, , bChar
    If Hex$(bChar) >= "C0" And _
    Hex$(bChar) <= "C3" Then
    Get #fnum, , bChar
    Get #fnum, , bChar
    Get #fnum, , bChar

    Get #fnum, , bChar
    a = bChar
    Get #fnum, , bChar
    b = bChar
    Get #fnum, , bChar
    c = bChar
    Get #fnum, , bChar
    D = bChar

    ImgHeight = CInt(a * 256 + [img]/forums/images/smilies/cool.gif[/img]
    ImgWidth = CInt(c * 256 + D)
    blExit = True
    Else
    If Hex$(bChar) = "DA" Then
    blExit = True
    Else
    Get #fnum, , bChar
    E = bChar
    Get #fnum, , bChar
    f = bChar
    MarkerLen = (E * 256 + f) - 2

    Dim marker As String
    marker = String(MarkerLen, vbNullChar)
    Get #fnum, , marker
    End If
    End If
    Wend

    Close #fnum

    With ImageInfo
    .Width = ImgWidth
    .Height = ImgHeight
    End With

    GetJPGInfo = ImageInfo
    End Function</pre>


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Read JPG Header Comment (VB6)

    The code you posted will retrieve the height and width of a .jpg file. I don't know anything about the way comments are stored inside the header of a .jpg file, so I can't tell you how to get at the comment. Does the software you use for adding the comments allow you to retrieve them?

    The code in your post can be used like this:

    - Put text boxes txtFilename, txtHeight and txtWidth on a form.
    - Put a command button cmdGetInfo on the form.
    - In the On Click event of the command button, retrieve height and width of the .jpg whose name is in txtFilename.
    - Put the result in the other text boxes.

    Private Sub cmdGetInfo_Click()
    Dim bi As BITMAPINFO
    bi = GetJPGInfo(txtFilename)
    txtHeight = bi.Height
    txtWidth = bi.Width
    End Sub

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read JPG Header Comment (VB6)

    Hans
    The software is very limited "edJpgCom.exe" but very useful to say the least.
    On double clicking the image (as visited earlier) the user is able to add a comment to the header.
    The image is then saved with new header information and over-writes the image.
    The only draw back, there's no visual way to show the user that there is a comment attached other than double clicking each image.
    If I could access this comment information, I'm sure I could build something into the array to perhaps show an icon of some sort next to
    the image.

    I'll try your suggestion anyway and play with the modul to see if there is any other info which can be accessed.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Read JPG Header Comment (VB6)

    Dave,

    I found some info about the way comments are stored. Could you attach a small .jpg that contains comments? That would allow me (and others) to experiment. Thanks in advance.

  5. #5
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read JPG Header Comment (VB6)

    Hans
    No problem at all <img src=/S/bouncenburn.gif border=0 alt=bouncenburn width=31 height=31>

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Read JPG Header Comment (VB6)

    Is the comment "image33" ?

  7. #7
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read JPG Header Comment (VB6)

    <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23>

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Read JPG Header Comment (VB6)

    Here you go. Oliver Fromme from Germany has written a clear description of the JPG header structure; you can find it (for example) here. With this description, it wasn't hard to adapt the code you posted to return the comment instead of the height and width. The new code is attached in a text file

    Use as follows:

    txtComment = GetJPGComment(txtFilename)

    where txtFilename is a text box containing the full path and filename of a .JPG file, and txtComment a text box to display the comment.

  9. #9
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read JPG Header Comment (VB6)

    Hans
    You just love a challenge ?? <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>

    I think I can use this now to show an icon next to the pic (excuse typo)

    <pre>Dim i as integer</pre>


    <pre>If Me.Controls("txtComment" & i).Text = vbNullString Then
    Me.Icon? .Visible = False
    Else
    Me.Icon?.Visible = True</pre>


    I'll try to build something into the FillImages Array using the "For" and "Next" statements.
    When the user see's the icon, he knows there is a comment attached.

    What do you think ?

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Read JPG Header Comment (VB6)

    You could put a series of icons on the form, named Icon1, Icon2, etc., and refer to them as Me.Controls("Icon" & i)
    Alternatively, you could use check boxes chkComment1, chkComment2, etc.

    If Me.Controls("txtComment" & i).Text = vbNullString Then
    Me.Controls("chkComment" & i) = 0
    Else
    Me.Controls("chkComment" & i) = 1
    End If

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Read JPG Header Comment (VB6)

    Not that you need another method, but I stumbled across this the other day and managed to find it again: Retrieving Extended File Properties from Windows 2000 Scripting Guide on MSDN. I wonder whether the Comments field matches up with the internal Comments field in the JPG file?

  12. #12
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read JPG Header Comment (VB6)

    Edited - Added graphic to try to make this clearer

    Jumping late here (as usual) with some information I dug out from an old post I made to somebody:
    "I have had cause to dig into the JPEG internal format & header information (to extract the metrics actually) but recall the existence of a well defined internal comment field. If this is the "comment" you're referring to, then I can't see why the task you describe would be hard at all (to program).

    JPEGs are full of markers, prefixed by FF (hexadecimal). Comment marker is identifiable as FFFE

    The first two bytes, the length, after the FF marker indicate the number of bytes, including the two length bytes, that this header contains. The next bytes contain whatever the user wants to use as a comment."

    This might help as a summary of the relevant information. The full standard can be viewed here. You (generic) can get a good idea of the way the JPEG markers work by viewing one in a hex editor but I see you've already zeroed in on it Hans <img src=/S/grin.gif border=0 alt=grin width=15 height=15>. I may "lift" your code for future reference.

    Alan

  13. #13
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read JPG Header Comment (VB6)

    Thanks all for the extended contribution and information.
    (Sorry I couldn't reply late yesterday, connection problems here and couldn't log on to the site ( Server Re-Boot sorted it))

    I have the application running now as I wanted it to.
    By checking the string of hidden controls, I can now show an icon for each image with a comment:

    <pre>(From FillImages Function)
    Me.Controls("txtComment" & i).Text = GetJPGComment("L:mmpdfimage" & _
    (Left$(arrFileNames(i + intIndex - 1, 1), 8) & ".jpg"))

    Dim E as Integer
    For E = 1 To intNumberOfImages
    If Me.Controls ("txtComment" & E).Text = vbNullString Then
    Me.Controls("check" & E).Visible = False
    Else
    Me.Controls("check" & E).Visible = True
    End If
    Next</pre>


    All I have to do now is to trim spaces from the txtComment text box's to make the automation more efficient.
    Again, Thanks all:

  14. #14
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Read JPG Header Comment (VB6)

    Where to trim from ?

    I've tried a couple of combinations, but nothing but the delete button works:

    Me.Controls("txtComment" & i).Text = GetJPGComment(Trim$("L:mmpdfimage" & (Left$(arrFileNames(i + intIndex - 1, 1), 8) & ".jpg")))
    ::
    Me.Controls("txtComment" & i).Text = (Trim$GetJPGComment("L:mmpdfimage" & (Left$(arrFileNames(i + intIndex - 1, 1), 8) & ".jpg")))
    ::

    Dim TrStr as String

    For E = 1 To intNumberOfImages
    TrStr = Me.Controls(Trim$("txtComment" & E)).Text
    If Me.Controls(TrStr).Text = vbNullString Then
    Me.Controls("check" & E).Visible = False
    Else
    Me.Controls("check" & E).Visible = True
    End If
    Next

  15. #15
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Read JPG Header Comment (VB6)

    In the first part, you want to trim the comment, not the file name:

    Me.Controls("txtComment" & i).Text = Trim$(GetJPGComment("L:mmpdfimage" & Left$(arrFileNames(i + intIndex - 1, 1), 8) & ".jpg"))

    In the second part, you want to trim the text in the control, not the name of the control:

    TrStr = Trim$(Me.Controls("txtComment" & E).Text)
    If TrStr = vbNullString Then
    ...

Page 1 of 2 12 LastLast

Posting Permissions

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