copy

Sunday, 18 February 2018



Last updated: 19/02/2018, 2 min read (without the code)

Get Image Size In Pixels With VBA


Introduction



People who are interested in digital photography taking seriously the size of the images that they take. The dimensions of an image – width and height – can be easily spotted in the Windows Explorer. If you hover your mouse over an image file you will see a pop window showing its dimensions.

Get Image Size By Hovering

Alternatively, you can right-click on an image file, select properties on the menu that pop-ups and then, in the Properties form, go to the Details tab. Close to the end, there is the image section where the dimensions, the width and height of the image is shown (in pixels).

Get Image Size Using File Properties

But, is there a way to get programmatically the size of an image with VBA? The answer is YES, and, unlike a previous post, does NOT involve Photoshop!



VBA code



The GetImageSize function uses the Microsoft Windows Image Acquisition Library v2.0 in order 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 respectively.

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:        xristos.samaras@gmail.com
    '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.

Get Image Size Function Examples

The second sheet (Batch Mode) can be used to get the 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 form a folder, use this free tool.

Get Image Size In Batch Mode



Downloads



Download

The zip file contains a workbook with the two worksheets that 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.

Did you like this post? If yes, then share it with your friends. Thank you!


Categories:


Mechanical Engineer (Ph.D. cand.), M.Sc. Cranfield University, Dipl.-Ing. Aristotle University, Thessaloniki - Greece.
Communication: e-mail, Facebook, Twitter, Google+ and Linkedin. More info