Get Image Resolution via VBScript

vbscript, windows

Solution

You gan get the image DPI using the `HorizontalResolution` and `VerticalResolution` properties of the `WIA.ImageFile` scripting object:

Set objImage = CreateObject("WIA.ImageFile")

objImage.LoadFile "C:\M.jpg"

If objImage.HorizontalResolution > 100 Then
  Log.Message "GOODQ (" & objImage.HorizontalResolution & " DPI)"
End If

Just for completeness, there's another solution for Windows Vista and later — using the `Folder.GetDetailsOf` method to read the image DPI from the extended file properties. But the code will be longer and a bit messier, because:

- The index of the Horizontal resolution and Vertical resolution file properties are different on different Windows versions (see this page and this my answer for details).

The extended file properties return DPI as a string like 240 dpi; you may need to convert it to a number.

' For Windows 7
Const HORIZONTAL_RESOLUTION = 161
Const VERTICAL_RESOLUTION = 163

Dim objShell : Set objShell = CreateObject("Shell.Application")
Dim objFolder : Set objFolder = objShell.Namespace("C:\MyFolder")
Dim objFile : Set objFile = objFolder.ParseName("M.jpg")

Dim strDpi : strDpi = objFolder.GetDetailsOf(objFile, HORIZONTAL_RESOLUTION) ' Returns DPI as a string like "240 dpi"
Dim dpi : dpi = ToInt(strDpi)

If dpi > 100 Then
  Log.Message "GOODQ (" & dpi & " DPI)"
End If

' Extracts a number from a string, e.g. "240 dpi" -> 240
' NB: no error handling
Function ToInt(ValueStr)
  Dim objRE : Set objRE = New RegExp
  objRE.Pattern = "\d+"
  Dim colMatches : Set colMatches = objRE.Execute(ValueStr)
  ToInt = CLng(colMatches(0).Value)
End Function

Problem

How can I get an image resolution in DPI with VBScript? for example ``` Res= GET "M.jpg" Resolution If Res > 100 Echo "GOODQ" ```

Original source

Related problems