Learn how to convert popular graphic files such as JPEG, GIF or PNG to the new HD Photo format from Microsoft. HD Photo debuted in Windows Vista, with the file extension WDP and recently HDP.
The new HD Photo file format from Microsoft is available in Windows Vista and included in .NET Framework 3.0. It offers better quality than JPEG at (most of the time) half the file size.
The file format has recently had its name changed from Windows Media Photo to HD Photo. Also, aside from WDP which we will be using in this tutorial, a new file extension will be handled by HD Photo: HDP. We are sticking to WDP in this tutorial since the HDP extension was introduced only recently and its thus not recognized by most of the software that handles HD Photos, such as the initial release of Windows Vista. WDP and HDP are one and the same, the only different is in the name of the final extension.
For more information on HD Photo, see Bill Crow’s HD Photo Blog at MSDN.com. He talks about the purpose of the new HD Photo format:
We’ve very specifically targeted printing with support for CMYK and n-Channel pixel formats. Â In fact, Windows Media Photo is the preferred image format for XML Paper Specification (XPS), the new printer spool file and portable document format being launched with Windows Vista. Â Windows Media Photo is also designed to be a great web image format, with support for excellent compression, alpha channels, region decode, progressive decode and high performance compressed domain operations. Â Of course, any data format for the web requires very broad support from a variety of client and server components. Â As a brand new format, it will take a little time for this support to develop for Windows Media Photo. Â However, it’s already in development for several important “behind the scenes” web tasks from both Microsoft and our partners.
- Bill Crow, Program Manager for Windows Media Photo
In order to get this tutorial and the source-code attached to it working, you will need to run Windows Vista or install .NET Framework 3.0 on your Windows XP or Windows Server 2003 machine. This will give you access to two DLLs that are crucial for the application to do its job.
Start by creating a new project in Visual Studio 2005 and adding the following as a reference: PresentationCore and WindowsBase. You’ll need a little help in adding these, so here it goes: in the Add Reference window, go to the Browse tab and navigate to C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\ – there you will see PresentationCore.dll which you need to add as a reference. The next DLL you need to add as a reference is in the same path – C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\ – and its name is WindowsBase.dll
Now that you added the PresentationCore.dll and WindowsBase.dll references to your Visual Studio 2005 Windows Project, don’t forget the two extra using statements:
using System.IO;
using System.Windows.Media.Imaging;
On the form add a button named btnConvert, an OpenFileDialog openPicture and a SaveFileDialog saveWdp. Now double-click btnConvert and you’ll get to its click event. Inside it we will open the proper dialogs:
private void btnConvert_Click(object sender, EventArgs e)
{
   // Prompt to open the file
   if (openPicture.ShowDialog() == DialogResult.OK)
   {
      // Prompt to save the file
      if (saveWdp.ShowDialog() == DialogResult.OK)
      {
         // Call the method that does all the work
         FileToWmp(openPicture.FileName, saveWdp.FileName);
      }
   }
}
Obviously, next comes the FileToWmp() method:
public static void FileToWmp(string inFile, string outFile)
{
   // Container for bitmap frames
   BitmapDecoder bdFile = null;
   // Read the source file into a FileStream object
   FileStream readFile = File.OpenRead(inFile);
   // Set the BitmapDecoder object from the source file
   bdFile = BitmapDecoder.Create(readFile, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None);
   // Prepare the output file
   FileStream writeFile = File.OpenWrite(outFile);
   // All the magic done by WmpBitmapEncoder
   WmpBitmapEncoder wbeFile = new WmpBitmapEncoder();
   // Set the quality level to... pretty good
   wbeFile.ImageQualityLevel = 0.9f;
   // Add the bitmap frame to the encoder object
   wbeFile.Frames.Add(bdFile.Frames[0]);
   // Write the output file
   wbeFile.Save(writeFile);
   writeFile.Close();
   readFile.Close();
}
Believe or not, that’s all you need to convert pretty much every popular file format – such as JPEG, GIF and PNG – to the brand new HD Photo format. And in most cases it will reduce the file size by over 50% too.