How Can I Determine the Height and Width of a Picture File Without Using Windows Media Player?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! Awhile back you showed us a method to determine the height and width of a picture file. However, that solution required us to import all our picture files into Windows Media Player. Isn’t there another way to get this information, one that doesn’t require Windows Media Player?

— RP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RP. You see, that’s what happens any time we Scripting Guys try to be clever. What with the burgeoning popularity of digital photography, scripters are very interested in retrieving the height and width of their image files using a script. Unfortunately, there’s no way – or at least no way built into the operating system – to gather that information. That’s a problem. So we came up with a nifty little workaround: if you import those pictures into Windows Media Player, then you can use Windows Media Player’s object model to get the height and width. Problem solved!

Or so we thought. As it turns out, though, not everyone was as excited as we were about importing all their picture files into Windows Media Player. (We should point out that this doesn’t move or modify the files; it just lets Windows Media Player know that those files exist.) On top of that, Windows Media Player lets you import (and thus get information about) only .JPG files. Do you have .GIF, .PNG, .TIF, or .BMP files you’d like to work with? Well, ….

Therefore, we’ve searched for another way to determine the height and width of a picture file. Does this one come with a catch, too? Unfortunately, yes: you’ll need to download and install the Windows Image Acquisition Automation Library 2.0. Once that’s installed, though, you’ll be able to determine the height and width of a picture file using four simple lines of code:

Set objImage = CreateObject(“WIA.ImageFile”)
objImage.LoadFile “C:\Scripts\Test.gif”

Wscript.Echo “Width: ” & objImage.Width Wscript.Echo “Height: ” & objImage.Height

Pretty easy, and it makes you wonder why this library isn’t just added to the operating system, doesn’t it? We can’t answer that one; all we can do is explain how the script works. As you can see, there isn’t much to it. We begin by creating an instance of the WIA.ImageFile object, then using the LoadFile method to bind to an image file. That’s very easy: all we have to do is pass LoadFile the path to the picture.

And then we simply echo back the values of the Width and Height properties. Like we said this works with .GIF, .PNG, .TIF, and .BMP files as well as .JPG files; in fact, if you look closely at our example code, you’ll see that we actually connected to a .GIF file. Will wonders never cease?

Incidentally, there are a few additional picture file properties you can retrieve using Windows Image Acquisition; for more information, take a peek at the WIA documentation on MSDN. And we promise: no more trying to be clever. We’ve learned our lesson!

0 comments

Discussion is closed.

Feedback usabilla icon