Learn about Windows PowerShell
This week saw the kickoff and first week of the 2010 Scripting Games. The Scripting Wife is in the midst of things, so we decided to give her a break and let her catch her breath. You can catch up with her progress by reviewing the articles in the archive.
Microsoft Scripting Guy Ed Wilson here. This week has been absolutely crazy. The first week of the 2010 Scripting Games are drawing to a close, and I have been busy grading hundreds of scripts that have been submitted. You can review those scripts, and pick up some awesome scripting techniques by popping over to PoshCode. You do not need to have a profile or be logged in to browse the scripts. Luckily, the scripting community has pitched in to aid in the grading, and we have more than a dozen judges.
When I was collecting the articles written by the expert commentators, many of the writers submitted their articles with images in .png format because that is the default setting for the Snipping Tool that comes with Windows Vista and Windows 7. The Snipping Tool is really cool because it allows you to take pictures of your desktop, a program window, or even a freeform snip. The .png format is really nice because it is a relatively compact format that provides good resolution. However, we use .jpg pictures in our blog.
Many of the commentators provided several pictures for their articles, and therefore I quickly ended up with a lot of pictures that I needed to convert to .jpg. Initially, I was copying the picture into Paint and using Save As to make the conversion because Paint in Windows 7 has been upgraded to make format conversions, but that quickly became a pain. I figured I could write a script to convert the file formats, but I had never seen such a script, and I was not even certain it could be done. So I decided to spend a little time on MSDN to see if there was a .NET Framework class I could use in Windows PowerShell 2.0. The ConvertPngToJpg.ps1 script is the result.
#Requires –Version 2.0$sourceFile = "C:\fso\capture.png"$saveFile = "C:\fso\capture.jpg"Add-Type -AssemblyName system.drawing$imageFormat = "System.Drawing.Imaging.ImageFormat" -as [type]$image = [drawing.image]::FromFile($sourceFile)$image.Save($saveFile, $imageFormat::jpeg)
The first thing I do in the ConvertPngToJpg.ps1 script is specify the path to the source file and the path for the converted file:
$sourceFile = "C:\fso\capture.png"$saveFile = "C:\fso\capture.jpg"
The next thing I do is load the system.drawing .NET Framework assembly. This will provide access to the drawing.image .NET Framework class. In Windows PowerShell 2.0, the Add-Type cmdlet is used to load .NET Framework assemblies. In Windows PowerShell 1.0, you had to use Reflection, which was more complicated. When loading a .NET Framework assembly to provide access to a class, MSDN can be used to tell you which assembly contains the class you are interested in obtaining. This is important because at times the name of the assembly does not match the namespace that contains the class (in our example, the two do in fact match).
MSDN tells us that the save method from the image class uses a type enumeration value to tell it what image type to save. To work with this enumeration directly, I needed to create an instance of the ImageFormat type. Nearly a dozen different image formats are listed in this enumeration. The code that creates the ImageFormat type is seen here:
$imageFormat = "System.Drawing.Imaging.ImageFormat" -as [type]
To load the image that is to be converted, use the FromFile static method. The FromFile method accepts a single path to the file that will converted as shown here, but it can also accept a Boolean value that controls color management information:
$image = [drawing.image]::FromFile($sourceFile)
After the image is loaded, we save it while specifying the image format. There are other options that can be specified when saving the image, and they are detailed on MSDN. The code to save the image is shown here:
When the script runs, it creates a .jpg file from the .png file. The two images are seen in the folder shown in the following image.
If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at firstname.lastname@example.org or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys