问题描述
我有此链接:
我想要在单元格B2上获取此JPG URL的尺寸
(我不会不管如何获取它,单元格 B2
上可以是1920,单元格 C2
上可以是1080。
您需要对 URLDownloadToFile
进行API调用才能下载图像。在下面的示例中,我们将下载到temp文件夹 C:\Temp\
。
下载图像后,您将创建一个新的Shell对象,并最终使用 .ExtendedProperty()
属性获取文件尺寸
完成后下载文件后,您可以继续使用 Kill()
删除临时文件。
Option Explicit
#If VBA7然后
声明PtrSafe功能URLDownloadToFile Lib urlmon别名 URLDownloadToFileA _
(ByVal pCaller为long,ByVal szURL为String,ByVal szFileName为String,_
ByVal dw保留为Long,ByVal lpfnCB为Long) URLDownloadToFile Lib urlmon别名 URLDownloadToFileA _
(ByVal pCaller为long,ByVal szURL为String,ByVal szFileName为String,_
ByVal dw保留为Long,ByVal lpfnCB为Long) b $ b子测试()
Const tmpDir $ = C:\Temp\;
Const tmpFile $ = tmpPicFile.jpg
Debug.Print URLDownloadToFile(0,ActiveSheet.Range( A2)。Value,tmpDir& tmpFile,0,0)
ActiveSheet.Range( B2)。值= getFileDimensions(tmpDir,tmpFile)
杀死tmpDir& tmpFile
End Sub
私有函数getFileDimensions(filePath $,fileName $)As String
With New Shell32.Shell
With。命名空间(filePath).ParseName(fileName)
getFileDimensions = .ExtendedProperty( Dimensions)
以
结尾以
结尾函数
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
)
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()
.
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
这篇关于从URL获取图像文件尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!