Obtaining Image File Dimensions From URL

我只是一个虾纸丫 提交于 2019-12-23 05:11:02

问题


I have this link: https://s23527.pcdn.co/wp-content/uploads/2017/04/wine_speedlights_kit_lens.jpg.optimal.jpg

It is on Cell A2:

I want to get on Cell B2 the dimensions of the URL of this JPG

(I don't mind how to get it, it can be 1920 on cell B2 and 1080 on cell C2)


回答1:


You will need to make an API call to URLDownloadToFile to download your image. In the below example, we will download to the temp folder C:\Temp\.

Once your image is downloaded, you will create a new Shell object, and ultimately use the .ExtendedProperty() property to grab your file dimensions

After you have finished downloading your file, you can go ahead and delete the temporary file using Kill().

The below method uses Early Binding. You will need to set a reference to

Microsoft Shell Controls And Automation

By going to Tools -> References in the VBE menu

Option Explicit

#If VBA7 Then
    Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
            (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, _
            ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
#Else
    Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
            (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, _
            ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
#End If

Sub test()

    Const tmpDir$ = "C:\Temp\"
    Const tmpFile$ = "tmpPicFile.jpg"

    Debug.Print URLDownloadToFile(0, ActiveSheet.Range("A2").Value, tmpDir & tmpFile, 0, 0)
    ActiveSheet.Range("B2").Value = getFileDimensions(tmpDir, tmpFile)
    Kill tmpDir & tmpFile

End Sub

Private Function getFileDimensions(filePath$, fileName$) As String

    With New Shell32.Shell
        With .Namespace(filePath).ParseName(fileName)
            getFileDimensions = .ExtendedProperty("Dimensions")
        End With
    End With

End Function


来源:https://stackoverflow.com/questions/53575544/obtaining-image-file-dimensions-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!