如何确定tiff文件的图像尺寸?

时间:2011-02-01 18:43:47

标签: vbscript asp-classic tiff

我有一个vbscript脚本,我需要通过.tif文件目录进行咀嚼。对于每个文件,我需要确定文件是按比例分配为横向还是纵向。基本上,我需要能够获取每个文件的图像尺寸。因此,我已经看到一些人阅读文件文件头以从jpg或bmp文件中提取此类信息的示例。有人做过同样的事情来提取tiff文件的维度吗?

2 个答案:

答案 0 :(得分:9)

在VBScript中,您可以通过两种方式确定图像尺寸:

  1. 使用 WIA自动化库download linkMSDN documentationan excellent Hey, Scripitng Guy! article on the subject)。注册 wiaaut.dll 库后,可以使用以下简单代码:

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


  2. 使用GetDetailsOf方法读取相应的扩展文件属性。这是一个本机Windows脚本方法,因此不需要外部库;但代码更长:

    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
    

    此脚本输出如下内容:

      

    尺寸:2464 x 3248
      宽度:2464像素
      高度:3248像素

    因此,如果您需要普通数字,则必须从返回的字符串中提取它们。

    此方法还有另一个问题 - 属性索引(脚本开头的那些常量)在不同的Windows版本中有所不同,正如我在this answer中所解释的那样。以上脚本适用于Windows 7,但如果您使用其他Windows版本,或者您希望脚本在不同的Windows版本上运行,则需要使用特定于版本的索引。可用属性索引的最完整列表是here

答案 1 :(得分:1)

我建议使用Atalasoft's DotImage Photo强大的&免费!但是,它是一个.NET包,所以你可以做一些Regasm Magic来使它工作。在开始之前,请查看Use vb.net in VBScript

以下是获取尺寸所需的代码。

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