Introduction
People interested in digital photography take very seriously the image size they use since it can affect both the quality and quantity of their photos. The dimensions of an image – width & height – can be easily spotted in Windows Explorer. If you hover your mouse over an image file, you will see a pop window showing its dimensions.
Alternatively, you can right-click on an image file, select properties on the menu that pop-ups, and then go to the Details tab in the Properties form. Close to the end, there is the image section where the image’s dimensions (width & height) are shown (in pixels).
But is there a way to get the size of an image programmatically with VBA? The answer is YES, and, unlike a previous post, it does NOT involve Photoshop!
VBA function to get the image size
The GetImageSize function uses the Microsoft Windows Image Acquisition Library v2.0 to retrieve the necessary information from an image file. The function returns an array of integers that hold the image width and height in pixels. The FileExists and the IsValidImageFormat functions check if a file exists and if a given path corresponds to a valid image file format.
Option Explicit
Function GetImageSize(ImagePath As String) As Variant
'--------------------------------------------------------------------------------------
'Returns an array of integers that hold the image width and height in pixels.
'The first element of the array corresponds to the width and the second to the height.
'The function uses the Microsoft Windows Image Acquisition Library v2.0, which can be
'found in the path: C:\Windows\System32\wiaaut.dll
'However, the code is written in late binding, so no reference is required.
'Written By: Christos Samaras
'Date: 18/02/2018
'E-mail: [email protected]
'Site: http://www.myengineeringworld.net
'--------------------------------------------------------------------------------------
'Declaring the necessary variables.
Dim imgSize(1) As Integer
Dim wia As Object
'Check that the image file exists.
If FileExists(ImagePath) = False Then Exit Function
'Check that the image file corresponds to an image format.
If IsValidImageFormat(ImagePath) = False Then Exit Function
'Create the ImageFile object and check if it exists.
On Error Resume Next
Set wia = CreateObject("WIA.ImageFile")
If wia Is Nothing Then Exit Function
On Error GoTo 0
'Load the ImageFile object with the specified File.
wia.LoadFile ImagePath
'Get the necessary properties.
imgSize(0) = wia.Width
imgSize(1) = wia.Height
'Release the ImageFile object.
Set wia = Nothing
'Return the array.
GetImageSize = imgSize
End Function
Function FileExists(FilePath As String) As Boolean
'--------------------------------------------------
'Checks if a file exists (using the Dir function).
'--------------------------------------------------
On Error Resume Next
If Len(FilePath) > 0 Then
If Not Dir(FilePath, vbDirectory) = vbNullString Then FileExists = True
End If
On Error GoTo 0
End Function
Function IsValidImageFormat(FilePath As String) As Boolean
'----------------------------------------------
'Checks if a given path is a valid image file.
'----------------------------------------------
'Declaring the necessary variables.
Dim imageFormats As Variant
Dim i As Integer
'Some common image extentions.
imageFormats = Array(".bmp", ".jpg", ".gif", ".tif", ".png")
'Loop through all the extentions and check if the path contains one of them.
For i = LBound(imageFormats) To UBound(imageFormats)
'If the file path contains the extension return true.
If InStr(1, UCase(FilePath), UCase(imageFormats(i)), vbTextCompare) > 0 Then
IsValidImageFormat = True
Exit Function
End If
Next i
End Function
Sample workbook
The sample workbook contains two sheets: the first one (Function Examples) presents several ways of using the GetImageSize function directly from the worksheet. The examples involve the CELL, TRANSPOSE, and the INDEX function.
The second sheet (Batch Mode) can get the image size information of up to 100 different images. By inserting the image paths in column C, the width and height are automatically shown in columns D and E. If you need an easy way to retrieve the file paths from a folder, use this free tool.
Downloads
The zip file contains a workbook with the two worksheets presented above, the VBA code, and a sample image to play with it. The workbook can be opened with Excel 2007 or newer. Please enable macros before using it.
Read also
Get Image Size From A URL
Get External Hyperlinks From A Web Page
You’re welcome, Rafael!
Thank you Christos, your example has helped me to obtain the pixels.
‘Get the necessary properties.
…
imgSize(2) = Round(wia.HorizontalResolution, 0)