需要使用VBScript/ASP classic从图形文件读取宽度/高度 [英] Need to read width/height from graphics file with VBScript/ASP classic

查看:46
本文介绍了需要使用VBScript/ASP classic从图形文件读取宽度/高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要读取一个图形文件,并在VBScript(ASP)中获取宽度/高度.我发现了一个名为 gfxSpex 的软件包,这似乎是很多人使用的软件包,但GIF可以正确获得宽度,而不能获得高度.PNG根本不起作用,因为例程正在0-3中查找类型,而.png文件中的是%PN.

I need to read a graphics file and get the width/height in VBScript (ASP). I found a package called gfxSpex that seems to be what a lot of people use but the GIFs get the width right but not the height. PNGs don't work at all as the routine is looking for the type in 0-3 and that's %PN in the .png files.

Function gfxSpex(flnm, width, height, depth, strImageType)
    Dim strPNG
    Dim strGIF
    Dim strBMP
    Dim strType
    strType = ""
    strImageType = "(unknown)"

    gfxSpex = False

    strPNG = Chr(137) & Chr(80) & Chr(78)
    strGIf = "GIF"
    strBMP = Chr(66) & Chr(77)

    strType = GetBytes(flnm, 0, 3)

    If strType = strGIf Then                ' is GIF
        strImageType = "GIF"
        Width = lngConvert(GetBytes(flnm, 7, 2))
        Height = lngConvert(GetBytes(flnm, 9, 2))
        Depth = 2 ^ ((Asc(GetBytes(flnm, 11, 1)) And 7) + 1)
        gfxSpex = True
    ElseIf Left(strType, 2) = strBMP Then       ' is BMP
        strImageType = "BMP"
        Width = lngConvert(GetBytes(flnm, 19, 2))
        Height = lngConvert(GetBytes(flnm, 23, 2))
        Depth = 2 ^ (Asc(GetBytes(flnm, 29, 1)))
        gfxSpex = True
    ElseIf strType = strPNG Then            ' Is PNG
        strImageType = "PNG"
        Width = lngConvert2(GetBytes(flnm, 19, 2))
        Height = lngConvert2(GetBytes(flnm, 23, 2))
        Depth = getBytes(flnm, 25, 2)

        Select Case Asc(right(Depth,1))
            Case 0
                Depth = 2 ^ (Asc(left(Depth, 1)))
                gfxSpex = True
            Case 2
                Depth = 2 ^ (Asc(left(Depth, 1)) * 3)
                gfxSpex = True
            Case 3
                Depth = 2 ^ (Asc(left(Depth, 1)))  '8
                gfxSpex = True
            Case 4
                Depth = 2 ^ (Asc(left(Depth, 1)) * 2)
                gfxSpex = True
            Case 6
                Depth = 2 ^ (Asc(left(Depth, 1)) * 4)
                gfxSpex = True
            Case Else
                Depth = -1
        End Select
    Else
        strBuff = GetBytes(flnm, 0, -1)     ' Get all bytes from file
        lngSize = Len(strBuff)
        flgFound = 0

        strTarget = Chr(255) & Chr(216) & Chr(255)
        flgFound = InStr(strBuff, strTarget)

        If flgFound = 0 Then
            Exit Function
        End If

        strImageType = "JPG"
        lngPos = flgFound + 2
        ExitLoop = False

        Do While ExitLoop = False And lngPos < lngSize
            Do While Asc(Mid(strBuff, lngPos, 1)) = 255 And lngPos < lngSize
                lngPos = lngPos + 1
            Loop

            If Asc(Mid(strBuff, lngPos, 1)) < 192 Or Asc(Mid(strBuff, lngPos, 1)) > 195 Then
                lngMarkerSize = lngConvert2(Mid(strBuff, lngPos + 1, 2))
                lngPos = lngPos + lngMarkerSize  + 1
            Else
                ExitLoop = True
            End If
        Loop

        If ExitLoop = False Then
            Width = -1
            Height = -1
            Depth = -1
        Else
            Height = lngConvert2(Mid(strBuff, lngPos + 4, 2))
            Width = lngConvert2(Mid(strBuff, lngPos + 6, 2))
            Depth = 2 ^ (Asc(Mid(strBuff, lngPos + 8, 1)) * 8)
            gfxSpex = True
        End If
    End If
End Function

Function GetBytes(flnm, offset, bytes)
    Dim objFSO
    Dim objFTemp
    Dim objTextStream
    Dim lngSize

    On Error Resume Next

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' First, we get the filesize
    Set objFTemp = objFSO.GetFile(flnm)
    lngSize = objFTemp.Size
    Set objFTemp = Nothing

    fsoForReading = 1
    Set objTextStream = objFSO.OpenTextFile(flnm, fsoForReading)

    If offset > 0 Then
        strBuff = objTextStream.Read(offset - 1)
    End If

    If bytes = -1 Then      ' Get All!
        GetBytes = objTextStream.Read(lngSize)  'ReadAll
    Else
        GetBytes = objTextStream.Read(bytes)
    End If

    objTextStream.Close
    Set objTextStream = Nothing
    Set objFSO = Nothing
End Function

Function lngConvert(strTemp)
    lngConvert = CLng(Asc(Left(strTemp, 1)) + ((Asc(Right(strTemp, 1)) * 256)))
End Function


Function lngConvert2(strTemp)
    lngConvert2 = CLng(Asc(Right(strTemp, 1)) + ((Asc(Left(strTemp, 1)) * 256)))
End Function

有人使用此 gfxSpex 函数并且他们对其进行了修改吗?有没有更好的方法来获取宽度和高度?

Is anyone using this gfxSpex function and have they modified it? Is there a better way to get the width and height?

推荐答案

是的,我不确定为什么删除评论!基本上,它给了我https://docs.microsoft.com/zh-CN/windows-hardware/drivers/image/wia-image-processing-filter .它比我需要的要多得多,但很高兴知道它在那里.我刚刚用过:

Yes, I'm not sure why the comment was deleted! Basically, it gave me a link to https://docs.microsoft.com/en-us/windows-hardware/drivers/image/wia-image-processing-filter. It's a lot more than I needed but good to know it's there. I just used:

set oIMG = CreateObject("WIA.ImageFile")
oIMG.loadFile(path)
iHeight = oIMG.Height
iWidth = oIMG.Width
set oIMG = nothing

它适用于gif,jpg和png.我什至不需要注册.

It worked for gif, jpg, and png's. I didn't even need to register it.

这篇关于需要使用VBScript/ASP classic从图形文件读取宽度/高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆