从 URL 获取图像文件尺寸
Obtaining Image File Dimensions From URL
我有这个link:
https://s23527.pcdn.co/wp-content/uploads/2017/04/wine_speedlights_kit_lens.jpg.optimal.jpg
在单元格 A2 上:
我想在单元格 B2 上获取此 JPGURL 的尺寸
(我不介意如何获取,它可以在 B2
单元格上为 1920,在单元格 C2
上为 1080)
您将需要 API 调用 URLDownloadToFile
来下载您的图片。在下面的示例中,我们将下载到临时文件夹 C:\Temp\
.
下载图片后,您将创建一个新的 Shell 对象,并最终使用 .ExtendedProperty()
属性 获取文件尺寸
下载完文件后,您可以使用 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
我有这个link: https://s23527.pcdn.co/wp-content/uploads/2017/04/wine_speedlights_kit_lens.jpg.optimal.jpg
在单元格 A2 上:
我想在单元格 B2 上获取此 JPGURL 的尺寸
(我不介意如何获取,它可以在 B2
单元格上为 1920,在单元格 C2
上为 1080)
您将需要 API 调用 URLDownloadToFile
来下载您的图片。在下面的示例中,我们将下载到临时文件夹 C:\Temp\
.
下载图片后,您将创建一个新的 Shell 对象,并最终使用 .ExtendedProperty()
属性 获取文件尺寸
下载完文件后,您可以使用 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