Hey, Scripting Guy! How Can I Use Windows PowerShell to Convert Graphics Files to Different File Formats?

Hey, Scripting Guy! I have a number of graphics files that I need to convert to a different file format. Can I use Windows PowerShell 2.0 to convert a .bmp file to a .jpg file?

-- SW

Hello SW,

Microsoft Scripting Guy Ed Wilson here. It is Guest Blogger Week here on the Script Center. Today, we have William Stanek talking about working with graphic files.

Take it away William…

I finished up work on Windows PowerShell 2.0 Administrator's Pocket Consultant awhile back. Recently, the Scripting Guys asked me if I could write some guest columns, so I began looking at what I'd written already and what I could write about. For starters, how about image conversion and manipulation using System.Drawing.Bitmap? Using System.Drawing.Bitmap, you can convert to or from BMP, GIF, JPEG, PNG, TIFF, and WMF formats.

The following example loads the required .NET Framework assemblies, gets an object reference for the image, and then displays the height and width of the image. The variable $i holds the image reference:

Here, the test image is in your working directory and called test.tiff. You can substitute whatever you've named your image (or reference a file path). Now that you have an object reference, you can work with the image. You could rotate the image 90 degrees:

$i.rotateflip("Rotate90FlipNone");

Alternatively, rotate 180 degrees using Rotate180FlipNone or rotate 270 degrees using Rotate270FlipNone. Also, you can FlipX, FlipY, or FlipXY instead of FlipNone. The complete set of options is as follows:

Name

Description

RotateNoneFlipNone

Uses no rotation and no flipping.

Rotate90FlipNone

Uses a 90-degree rotation without flipping.

Rotate180FlipNone

Uses a 180-degree rotation without flipping.

Rotate270FlipNone

Uses a 270-degree rotation without flipping.

RotateNoneFlipX

Uses no rotation followed by a horizontal flip.

Rotate90FlipX

Uses a 90-degree rotation followed by a horizontal flip.

Rotate180FlipX

Uses a 180-degree rotation followed by a horizontal flip.

Rotate270FlipX

Uses a 270-degree rotation followed by a horizontal flip.

RotateNoneFlipY

Uses no rotation followed by a vertical flip.

Rotate90FlipY

Uses a 90-degree rotation followed by a vertical flip.

Rotate180FlipY

Uses a 180-degree rotation followed by a vertical flip.

Rotate270FlipY

Uses a 270-degree rotation followed by a vertical flip.

RotateNoneFlipXY

Uses no rotation followed by a horizontal and vertical flip.

Rotate90FlipXY

Uses a 90-degree rotation followed by a horizontal and vertical flip.

Rotate180FlipXY

Uses a 180-degree rotation followed by a horizontal and vertical flip.

Rotate270FlipXY

Uses a 270-degree rotation followed by a horizontal and vertical flip.

The working copy of the image is not saved yet to the file system. You can save the modified image in BMP, GIF, JPEG, PNG, TIFF, or WMF format. Keep in mind that you may lose quality going from a format such as TIFF to a format such as JPEG. The following example saves in GIF format:

$i.Save("test.gif","GIF");

To display the properties of the image object you are working with, use:

$i;

To display the complete set of methods and properties for System.Drawing.Bitmap objects, use:

After running the script, the figure is flipped and the picture format is changed. A .gif file is associated with Internet Explorer by default. The new image is shown here.

Although image manipulation isn't really an administrative feature discussed in the Windows PowerShell 2.0 Administrator's Pocket Consultant, the .NET Framework is. Hopefully, this is fun for you to play with and helps you get started with the .NET Framework and objects.