Microsoft Windows Image Acquisition (WIA) Automation Layerは画像を操作するライブラリです。 VBAの参照設定を行うことでPNG画像やJPG画像の情報を取得することができます。
Microsoft Windows Image Acquisition Automation Layerの目的(Purpose)の部分を参照してみると 画像の処理用ライブラリであることが書かれてます。
The Windows Image Acquisition (WIA) Automation Layer is a full-featured image manipulation component that provides end-to-end image processing capabilities
(Windows Image Acquisition (WIA) Automation Layerは、エンドツーエンドの画像処理機能を提供するフル機能の画像処理コンポーネントです。)
画像情報取得する #
C:\wiatestフォルダ内にpngとjpgの画像を置きました。
- 幅100x高さ150のpng画像
- 幅200x高さ250のjpg画像
Windows Image Acquisition Automationの機能を使用するには参照設定が必要です。 Microsoft Windows Image Acquisition Libraryにチェックをつけます。
ImageFileオブジェクト
を使用して画像を取り込み、PNG画像とJPG画像の幅と高さをそれぞれ取得して
イミディエイトウィンドウに表示します。
Option Explicit
Sub PrintPictureInfo()
Dim pngImg As New ImageFile
pngImg.LoadFile "C:\wiatest\100x150.png"
Debug.Print "PNG画像の幅:" & pngImg.Width
Debug.Print "PNG画像の高さ:" & pngImg.Height
Dim jpgImg As New ImageFile
jpgImg.LoadFile "C:\wiatest\200x250.jpg"
Debug.Print "JPG画像の幅:" & jpgImg.Width
Debug.Print "JPG画像の高さ:" & jpgImg.Height
Set pngImg = Nothing
Set jpgImg = Nothing
End Sub
イミディエイトウィンドウにPNG画像とJPG画像の幅と高さが出力されます。
PNG画像の幅:100
PNG画像の高さ:150
JPG画像の幅:200
JPG画像の高さ:250
画像の幅と高さをシートに記入する #
Sheet1シートのA列2行以降に画像パスを記入しました。
この画像パスを元に画像の幅と高さを取得し、B列に画像の幅、C列に画像の高さを記入してみます。
Option Explicit
'画像の幅と高さを保存する構造体
Type PictureInfo
Width As Long
Height As Long
End Type
Function GetPictureInfo(path As String) As PictureInfo
Dim pi As PictureInfo
Dim img As New ImageFile
img.LoadFile path
pi.Width = img.Width
pi.Height = img.Height
GetPictureInfo = pi
Set img = Nothing
End Function
GetPictureInfo
を呼び出してシートに画像の幅と高さを記入してみます。
Sub TestGetPictureInfo()
Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets("Sheet1")
Dim pi As PictureInfo
pi = GetPictureInfo(sht.Cells(2, 1))
sht.Cells(2, 2) = pi.Width
sht.Cells(2, 3) = pi.Height
pi = GetPictureInfo(sht.Cells(3, 1))
sht.Cells(3, 2) = pi.Width
sht.Cells(3, 3) = pi.Height
End Sub
それぞれの画像の幅と高さがB列とC列に記入されました。