Results 1 to 10 of 10

Thread: how to get image sizes

  1. #1

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    how to get image sizes

    Is it possible to get the image size of jpg images using vb6? In the following code I get the names of the images in my folder. I would like to ad a routine that would also get the image size for each image. I wasn't sure if i could even do this with this code. Any help appreciated.

    VB Code:
    1. Dim nReplace As String
    2.     Dim folderContent As String
    3.  
    4.     For Each fldr In myFolders
    5.         Set myFiles = fldr.Files
    6.         folderContent = vbNullString
    7.         i = 0
    8.         For Each f In myFiles
    9.             If VBA.Right$(f.Name, 4) = ".jpg" Or VBA.Right$(f.Name, 4) = ".JPG" Then
    10.                 'append file names
    11.                 folderContent = (folderContent & "&pic" & i & "=" & f.Name)
    12.                 i = i + 1
    13.             End If
    14.         Next f
    15.         'prepend file count
    16.         nReplace = "&Rc=" & i & folderContent
    17.         Set ts = fso.OpenTextFile(App.Path & "\image_txt_files\" & fldr.Name & ".txt", ForWriting, True, TristateFalse)
    18.         ts.Write (nReplace)
    19.         ts.Close    'close each file, not just the last one
    20.     Next fldr
    He who never made a mistake never made a discovery?

  2. #2
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: how to get image sizes

    Image size? Like the height and width?

    If that's what you mean you can load the picture, then just do something like:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim height As Integer
    3.     Dim length As Integer
    4.    
    5.         height = Image1.Height
    6.         length = Image1.width
    7.        
    8.     MsgBox "Image is " & height & "x" & length
    9.    
    10. End Sub

    But I'm not quite sure what you mean. So this could be no help..

  3. #3

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: how to get image sizes

    Yes, width and height is what I meant. I need to be abel to read thru the jpg images that are in the file and add the info to the text file. I don't want the user to know anything is being done.
    He who never made a mistake never made a discovery?

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: how to get image sizes

    You can load the jpg into a StdPicture object to get the height and width. The Height and Width values are in HiMetric units and will need to be converted.

    VB Code:
    1. 'this code must run within a Form Module
    2. Dim objPic as StdPicture
    3.  
    4. objPic = LoadPicture(FileName)
    5.  
    6. debug.print ScaleX(objPic.Width,vbHiMetric,vbPixels), ScaleY(objPic.Height,vbHiMetric,vbPixels)

    The better option would be to read the jpg file as it should contain the height and width values. All you need to know is where in the file these values are stored.

  5. #5

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: how to get image sizes

    Thanks for your suggestion.

    The better option would be to read the jpg file as it should contain the height and width values.
    I need to be abel to read thru the jpg images that are in the file and add the info to the text file. I don't want the user to know anything is being done.
    He who never made a mistake never made a discovery?

  6. #6

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: how to get image sizes

    ok, i found some code that is supposed to get the image size. I added to my project but the best i could do is get the image size of one image on my c drive. So I thought I would post the entire project, and maybe someone could help.

    The MsgBox ImageSize("c:\test.jpg"), returns the right value. I am not sure how to integrate it into my txt file code. I tried this:

    VB Code:
    1. folderContent = (folderContent & "&pic" & i & "=" & f.Name & "&imgsize" & i & "=" & ImageSize) '// something here //'

    But I get a complie error-argument not optional.

    VB Code:
    1. Option Explicit
    2. Dim fso As New FileSystemObject, ts As TextStream
    3. Dim myFiles As Object, i&, f As Object
    4. Dim Rc As Integer    'number of folders in directory
    5. Dim fldr As Scripting.Folder, myFolders As Scripting.Folders
    6. Dim iCount As Integer
    7.  
    8. Private Sub Command1_Click()
    9. 'select directory, write names of folders into txt file, write names of files from folders into txt files, add to folder image_txt_files
    10.  
    11. 'added code
    12. MsgBox ImageSize("c:\test.jpg")
    13. '*************'
    14.  
    15.     Screen.MousePointer = vbHourglass
    16.     DoEvents
    17.  
    18.     'create directory if it does not already exist
    19.     If Dir(App.Path & "\" & "image_txt_files", vbDirectory) = "" Then
    20.         'MsgBox "Directory doesn't exist"
    21.         MkDir (App.Path & "\" & "image_txt_files")    ' make new directory
    22.     Else
    23.         'MsgBox "Directory Exist"
    24.     End If
    25.  
    26.     'get folder count from Directory write folders.txt
    27.     For i = 0 To Dir1.ListCount - 1
    28.         Rc = Dir1.ListCount
    29.     Next i
    30.     Set myFolders = fso.GetFolder(Dir1.Path).SubFolders
    31.     Set ts = fso.OpenTextFile(App.Path & "\image_txt_files\folders.txt", ForWriting, True, TristateFalse)
    32.     ts.Write ("&Rc=" & myFolders.Count)
    33.     i = 0
    34.     For Each fldr In myFolders
    35.         ts.Write ("&Folder" & i & "=" & fldr.Name)
    36.         i = i + 1    'increment the counter
    37.     Next
    38.     ts.Close
    39.  
    40.     'write seperate folderName.txts for each folder
    41.     Set myFolders = fso.GetFolder(Dir1.Path).SubFolders
    42.     i = 0
    43.     For Each fldr In myFolders
    44.         Set ts = fso.OpenTextFile(App.Path & "\image_txt_files\" & fldr.Name & ".txt", ForWriting, True, TristateFalse)
    45.         i = i + 1    'increment the counter
    46.     Next
    47.     ts.Close
    48.  
    49.     Dim nReplace As String
    50.     Dim folderContent As String
    51.  
    52.     For Each fldr In myFolders
    53.         Set myFiles = fldr.Files
    54.         folderContent = vbNullString
    55.         i = 0
    56.         For Each f In myFiles
    57.             If VBA.Right$(f.Name, 4) = ".jpg" Or VBA.Right$(f.Name, 4) = ".JPG" Then
    58.                 'append file names
    59.                folderContent = (folderContent & "&pic" & i & "=" & f.Name & "&imgsize" & i & "=" & ImageSize) '// something here //'
    60.                 i = i + 1
    61.             End If
    62.         Next f
    63.         'prepend file count
    64.         nReplace = "&Rc=" & i & folderContent
    65.         Set ts = fso.OpenTextFile(App.Path & "\image_txt_files\" & fldr.Name & ".txt", ForWriting, True, TristateFalse)
    66.         ts.Write (nReplace)
    67.         ts.Close    'close each file, not just the last one
    68.     Next fldr
    69.  
    70.     Screen.MousePointer = vbNormal
    71.  
    72. End Sub
    73.  
    74. 'added code
    75. '*************'
    76. Private Function GetJPGSize( _
    77.  nW As Long, nH As Long, sFileName As String) As Boolean
    78.     Dim sHeader As String
    79.     Dim hFile As Integer
    80.     Dim nPos As Long
    81.     Dim n As Long
    82.     Dim c(3) As Long
    83.     Dim nMin As Long
    84.    
    85.     Dim sChar As String
    86.     Const MARKER As String = "JFIF" & vbNullChar
    87.     Const ID As String = "ÿØ"
    88.    
    89.     hFile = FreeFile
    90.     On Error Resume Next
    91.     Open sFileName For Binary Access Read As #hFile
    92.         If Err.Number Then
    93.             Exit Function
    94.         End If
    95.         If LOF(hFile) < 300 Then
    96.             Close #hFile
    97.             Exit Function
    98.         End If
    99.         'read the first 300 bytes
    100.         sHeader = Input(300, hFile)
    101.     Close #hFile
    102.     If Left$(sHeader, 2) <> ID Then
    103.         Exit Function
    104.     End If
    105.     nPos = InStr(sHeader, MARKER)
    106.     If nPos = 0 Or nPos + 7 > 291 Then
    107.         Exit Function
    108.     End If
    109.     c(0) = InStr(nPos + 5, sHeader, Chr(255) & Chr$(192))
    110.     If c(0) = 0 Then c(0) = 301
    111.     c(1) = InStr(nPos + 5, sHeader, Chr(255) & Chr$(193))
    112.     If c(1) = 0 Then c(1) = 301
    113.     c(2) = InStr(nPos + 5, sHeader, Chr(255) & Chr$(194))
    114.     If c(2) = 0 Then c(2) = 301
    115.     c(3) = InStr(nPos + 5, sHeader, Chr(255) & Chr$(195))
    116.     If c(3) = 0 Then c(3) = 301
    117.     nMin = 301
    118.     For n = 0 To 3
    119.         If c(n) < nMin Then
    120.             nMin = c(n)
    121.         End If
    122.     Next
    123.     If nMin > 291 Then
    124.         Exit Function
    125.     End If
    126.     sChar = Mid$(sHeader, nMin + 5, 2)
    127.     nH = Asc(Left$(sChar, 1)) * 256 + Asc(Right$(sChar, 1))
    128.     sChar = Mid$(sHeader, nMin + 7, 2)
    129.     nW = Asc(Left$(sChar, 1)) * 256 + Asc(Right$(sChar, 1))
    130.     GetJPGSize = True
    131. End Function
    132.  
    133. 'added code
    134. '*************'
    135. Public Function ImageSize(ByVal sFileName As String) As String
    136.     Dim nW As Long, nH As Long
    137.     Dim pic As StdPicture
    138.     If GetJPGSize(nW, nH, sFileName) Then
    139.         ImageSize = nW & "x" & nH
    140.     Else
    141.         On Error Resume Next
    142.         Set pic = LoadPicture(sFileName)
    143.         If Err.Number = 0 Then
    144.             nW = ScaleX(pic.Width, vbHimetric, vbPixels)
    145.             nH = ScaleY(pic.Height, vbHimetric, vbPixels)
    146.             ImageSize = nW & "x" & nH
    147.         End If
    148.     End If
    149. End Function
    150. Private Sub Drive1_Change()
    151.     Dir1.Path = Drive1.Drive
    152. End Sub
    He who never made a mistake never made a discovery?

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: how to get image sizes

    The ImageSize function requires a Filename argument. I did not do any testing but I think you just need to pass the value of f.Name

    folderContent = (folderContent & "&pic" & i & "=" & f.Name & "&imgsize" & i & "=" & ImageSize(f.Name))

  8. #8

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: how to get image sizes

    Hmm.. I tried your suggestion but the results were not written to the txt file. It looks like this:
    Code:
    &Rc=11&pic0=042AboutToTouchDown.jpg&imgsize0=&pic1=042andF-15OverNorfolkShippingYards.jpg&imgsize1=&pic2=042andF-15StillInPattern.jpg&imgsize2=&pic3=042BlindingShot.jpg&imgsize3=&pic4=042Inverted.jpg&imgsize4=&pic5=042LowerMissleDoorsOpen.jpg&imgsize5=&pic6=042OpeningMissleDoors.jpg&imgsize6=&pic7=042OverVirginia.jpg&imgsize7=&pic8=042SlicingThroughTheAir.jpg&imgsize8=&pic9=ComingFromMarietta.jpg&imgsize9=&pic10=ComingIn_ToLangley.jpg&imgsize10=
    He who never made a mistake never made a discovery?

  9. #9
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: how to get image sizes

    Pass f.Path to the ImageSize proceudre instead of f.Name.

  10. #10

    Thread Starter
    Fanatic Member Navarone's Avatar
    Join Date
    Jun 2003
    Location
    Akron, Ohio USA
    Posts
    740

    Re: how to get image sizes

    Don't you have to define f.Path first?
    He who never made a mistake never made a discovery?

Posting Permissions

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



Click Here to Expand Forum to Full Width