PDA

View Full Version : Determine media file pixel size



lifeson
12-19-2008, 03:35 AM
Hi folks
Is it possible using vba to determine ths dimensions (width and height) of a media file (I am looking at sizing a control based on the dimensions of the file)

Thanks

Jan Karel Pieterse
12-19-2008, 07:35 AM
This code lists all files with .jpg on the actve sheet. The pixel sizes aren't exactly right though.


Option Explicit
Dim iWidth, iHeight
Sub ImgDimension(img)
Dim myImg, fs
On Error Resume Next
iWidth = 9999
iHeight = 9999
Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.fileExists(img) Then Exit Sub
Set myImg = LoadPicture(img)
iWidth = Round(myImg.Width / 26.4583)
iHeight = Round(myImg.Height / 26.4583)
Set myImg = Nothing
End Sub
Sub ListImageDimensions()
Dim sFileName As String
Dim sPath As String
sPath = "C:\data\"
sFileName = Dir(sPath & "*.jpg")
While sFileName <> ""
ImgDimension sPath & sFileName
ActiveSheet.Range("A65536").End(xlUp).Offset(1).Value = sFileName
ActiveSheet.Range("A65536").End(xlUp).Offset(, 1).Value = iWidth
ActiveSheet.Range("A65536").End(xlUp).Offset(, 2).Value = iHeight
sFileName = Dir()
Wend
End Sub

lifeson
12-19-2008, 08:30 AM
Thanks Jan
With a bit of modifying thats worked well with image files (seems accurate to me regarding the sizes)
It errors though with mpg files
Any idea how to handle video files?