3

I have a vbscript script and I need to chew through a directory of .tif files. For each file, I need to determine if the file is proportioned as a landscape or a portrait. Basically, I need to be able to get the image dimensions for each file. So for, I have seen some examples of people reading the file file headers to extract this sort of information from jpg or bmp files. Has anyone done the same thing to extract the dimensions for a tiff file?

gun_shy
  • 485
  • 2
  • 7
  • 17
  • What else does your script need to do? If it's a standalone script, then you're far better off porting it to something else (.NET, Powershell) than trying to read TIFF files in VBScript. There used to be a free component called ImgSize.dll from ServerObjects that would do this, but it's been discontinued and is no longer available. – Dylan Beattie Feb 01 '11 at 21:26

2 Answers2

9

In VBScript, you can determine the image dimensions in two ways:

  1. Using the WIA Automation Library (download link, MSDN documentation, an excellent Hey, Scripitng Guy! article on the subject). Once you have the wiaaut.dll library registered, you can use the following simple code:

    Set oImage = CreateObject("WIA.ImageFile")
    oImage.LoadFile "C:\Images\MyImage.tif"
    
    WScript.Echo "Width: "  & oImage.Width & vbNewLine & _
                 "Height: " & oImage.Height
    


  2. Using the GetDetailsOf method to read the corresponding extended file properties. This is a native Windows scripting method, so no external libraries are required; but the code is longer:

    Const DIMENSIONS = 31
    CONST WIDTH  = 162
    CONST HEIGTH = 164
    
    Set oShell  = CreateObject ("Shell.Application")
    Set oFolder = oShell.Namespace ("C:\Images")
    Set oFile   = oFolder.ParseName("MyImage.tif")
    
    strDimensions = oFolder.GetDetailsOf(oFile, DIMENSIONS)    
    strWidth  = oFolder.GetDetailsOf(oFile, WIDTH)
    strHeigth = oFolder.GetDetailsOf(oFile, HEIGTH)
    
    WScript.Echo "Dimensions: " & strDimensions & vbNewLine & _
                 "Width: "      & strWidth      & vbNewLine & _
                 "Height: "     & strHeigth
    

    This script outputs something like:

    Dimensions: 2464 x 3248
    Width: 2464 pixels
    Height: 3248 pixels

    so if you need plain numbers, you'll have to extract them from the returned strings.

    There's also another problem with this method - the property indexes (those constants in the beginning on the script) are different in different Windows versions, as I explained in this answer. The above script is for Windows 7, but if you use another Windows versions or if you want the script to work on different Windows versions, you'll need to use version-specific indexes. The most complete list of available property indexes is here.

Community
  • 1
  • 1
Helen
  • 87,344
  • 17
  • 243
  • 314
0

I would recommend using Atalasoft's DotImage Photo Its powerful & it's free! But, it's a .NET package, so you'll have do some Regasm Magic to make it work. Go check out Use vb.net in VBScript before you get started.

Here is the code you'll need to get the dimensions.

Public Function GetHeight(path As String) As Integer

    Using stm As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim decoder As New TiffDecoder()
        If Not decoder.IsValidFormat(stm) Then
            Throw New Exception("not a TIFF")
        End If
        Dim image As AtalaImage = decoder.Read(stm, Nothing)
        Return image.Height
            ' Return image.Width --- To return the Width.

    End Using
End Function
Community
  • 1
  • 1
NeerPatel
  • 863
  • 6
  • 19
  • 1
    You are better off doing Dim info as ImageInfo = decoder.GetImageInfo(stm, Nothing) as this will allocate far less memory. Also be aware that TIFF is a multi page format and dotImage PhotoFree doesn't support multipage documents, but dotImage PhotoPro and dotImage document imaging do. – plinth Feb 01 '11 at 21:24