从URL获取图像文件尺寸 [英] Obtaining Image File Dimensions From URL

查看:76
本文介绍了从URL获取图像文件尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此链接:



我想要在单元格B2上获取此JPG URL的尺寸



(我不会不管如何获取它,单元格 B2 上可以是1920,单元格 C2 上可以是1080。

解决方案

您需要对 URLDownloadToFile 进行API调用才能下载图像。在下面的示例中,我们将下载到temp文件夹 C:\Temp\


下载图像后,您将创建一个新的Shell对象,并最终使用 .ExtendedProperty()属性获取文件尺寸


完成后下载文件后,您可以继续使用 Kill()删除临时文件。


下面的方法使用早期绑定。您将需要设置对以下内容的引用:

  Microsoft Shell控件和自动化

通过转到工具-> VBE菜单中的引用



  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().

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

这篇关于从URL获取图像文件尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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