Reading Basic Image Properties
This is a continuation of my notes on the WIA COM Object, which start with Working with images in VBA - Displaying PNG files
WIA provides an easy way to get access to basic images properties on the one hand, and more to both read and write more advanced image properties, such as EXIF MetaData and Animated GIF frames. Below, I set out a simple routine to demonstrate how to load an image into the WIA Object, and then access the various properties. This is not intended to be exhaustive. My suggestion would be to step through the code, and check out the WIA Object in the Object Browser in the VBA IDE once the image has loaded in order to get a good understanding.
The routines set out in Part 1 demonstrate how to convert various data sources into an stdPicture object. In order to get access to the image properties, though, it is necessary to convert the data into an ImageFile - one of the three main WIA Objects used for image manipulation/display in VBA. (the other two are Vector and ImageProcess). As such, I've had to write an additional function - GetImageFileFromURL (which bears a stunning similarity to one of the Part 1 functions - GetImageFromURL); this will read an image file from a URL.
Here is a test sub with that will process two files: (1) an image on the internet (I have used this one before, because it has EXIF Metadata on it); and
(2) an animated GIF file of a dancing panda... because why not. The static image is on the right hand side for your reference. The code envisages that you download the GIF image yourself (URL in the comments) so as to demonstrate how to load an image from your local drive using the LoadFile method, but you're welcome to use another image.
The following code should be placed in a standard module, and you're welcome to call it whatever you like - maybe modWorkingWithWhackyWIA or modDansDemos... or something less fun, like modWIA_Properties
This is a continuation of my notes on the WIA COM Object, which start with Working with images in VBA - Displaying PNG files
WIA provides an easy way to get access to basic images properties on the one hand, and more to both read and write more advanced image properties, such as EXIF MetaData and Animated GIF frames. Below, I set out a simple routine to demonstrate how to load an image into the WIA Object, and then access the various properties. This is not intended to be exhaustive. My suggestion would be to step through the code, and check out the WIA Object in the Object Browser in the VBA IDE once the image has loaded in order to get a good understanding.
The routines set out in Part 1 demonstrate how to convert various data sources into an stdPicture object. In order to get access to the image properties, though, it is necessary to convert the data into an ImageFile - one of the three main WIA Objects used for image manipulation/display in VBA. (the other two are Vector and ImageProcess). As such, I've had to write an additional function - GetImageFileFromURL (which bears a stunning similarity to one of the Part 1 functions - GetImageFromURL); this will read an image file from a URL.
Here is a test sub with that will process two files: (1) an image on the internet (I have used this one before, because it has EXIF Metadata on it); and
(2) an animated GIF file of a dancing panda... because why not. The static image is on the right hand side for your reference. The code envisages that you download the GIF image yourself (URL in the comments) so as to demonstrate how to load an image from your local drive using the LoadFile method, but you're welcome to use another image.
VBA Code:
Sub BasicTest()
Dim File1 As Object ' WIA.ImageFile
Dim File2 As Variant ' String - Filename
Debug.Print vbCrLf & "---------------- TEST 1 -----------------" & vbCrLf
Set File1 = GetImageFileFromURL("https://github.com/KallunWillock/JustMoreVBA/raw/main/Images/pexels-jill-evans-11567527.jpg")
BasicImageProperties_Test File1
Debug.Print vbCrLf & "---------------- TEST 2 -----------------" & vbCrLf
' https://tenor.com/view/hasher-happy-sticker-gif-24532177
' https://media.tenor.com/SaX1bE-Tug8AAAAi/hasher-happy-sticker.gif
' Source URL: TENOR GIF
File2 = "D:\hasher-happy-sticker.gif"
BasicImageProperties_Test File2
End Sub
The following code should be placed in a standard module, and you're welcome to call it whatever you like - maybe modWorkingWithWhackyWIA or modDansDemos... or something less fun, like modWIA_Properties
VBA Code:
Option Explicit
' WIA - accessing image properties
'
' The following function (and test procedure) demonstrates
' how to access basic image properties with the WIA COM Object.
' I have used late binding here, but you can enable
' early-binding (and intellisense) by selecting:
'
' Tools > References > Microsoft Windows Image Acquisition Library
'
' Dan_W
Enum ImagePropertyTypeEnum
ImageWidth
ImageHeight
HorizontalResolution
VerticalResolution
IsAnimated
ActiveFrame
FrameCount
FileExtension
HasTransparency
PixelDepth
End Enum
Function GetImageProperties(ByVal File As Variant, ByVal PropertyType As ImagePropertyTypeEnum)
Dim TargetImage As Object
If TypeName(File) = "IImageFile" Then
Set TargetImage = File
ElseIf TypeName(File) = "String" Then
If Len(Dir(File)) = 0 Then Exit Function
' Late-binding option
Set TargetImage = CreateObject("WIA.ImageFile")
' Early-binding option
' Dim TargetImage As New WIA.ImageFile
TargetImage.LoadFile File
End If
With TargetImage
GetImageProperties = Trim(Array(.Width, .Height, .HorizontalResolution, .VerticalResolution, _
CBool(.IsAnimated), .ActiveFrame, .FrameCount, .FileExtension, _
CBool(.IsAlphaPixelFormat), .PixelDepth)(PropertyType))
End With
Set TargetImage = Nothing
End Function
Sub BasicImageProperties_Test(ByVal File As Variant)
Dim PropertyHeadings As Variant
PropertyHeadings = Array("Width (pixels)", "Height (pixels)", "Horizontal Resolution", "Vertical Resolution", _
"Animated", "Active Frame", "Frame Count", "File Extension", _
"Is Alpha Pixel Format", "Pixel Depth")
Dim Counter As Long
Dim Heading As String * 22
For Counter = LBound(PropertyHeadings) To UBound(PropertyHeadings)
Heading = PropertyHeadings(Counter)
Debug.Print Counter + 1, Heading, GetImageProperties(File, Counter)
Next
End Sub
Function GetImageFileFromURL(ByVal TargetURL As String) As Object
Dim HTTP As Object
Set HTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
HTTP.Open "GET", TargetURL, False
HTTP.send
If HTTP.Status = 200 Then
With CreateObject("WIA.Vector")
.BinaryData = HTTP.responsebody
Set GetImageFileFromURL = .ImageFile
End With
End If
Set HTTP = Nothing
End Function