WPF : programming an image file conversion tool with Perspective

WPF : programming an image file conversion tool with Perspective

WPF and Windows Imaging Component (WIC)

WPF provides advanced image manipulation services, that I describe (in french) in my book .

These services are based on the native Windows Imaging Component (WIC) API. The standard file format codecs (BMP, JPEG, GIF, PNG, HD Photo, etc.) are installed by default under Windows Vista, and are installed under Windows XP with the .NET 3.x framework. For more exotic formats such as RAW formats of digital cameras, manufacturers provide WIC codecs to install. Examples :

Thanks to WIC, WPF provides an abstraction layer which make it possible to handle images with the same functions whatever their original or target format.

Starting from the 1.0 version, the Perspective library offers high-level classes (BitmapToJpegConverter, BitmapToPngConverter and BitmapConverter), which encapsulate the API to allow the conversion of image files in a source folder to files in another format created in a target folder.

ImageToJpeg

I have thus made a console application (ImageToJpeg.exe) to convert RAW files from my Nikon camera in JPEG files. RAW files contain raw data issued by the sensor. They constitute the original pictures, and have the same value as the traditional photo negatives. With this tool, I convert them to JPEG files specifying a compression ratio depending on use sought. The syntax is as follows:

ImageToJpeg.exe originalPath convertedPath [quality] [ow:0/1] [recurs:0/1]
- originalPath : path of the folder containing the original files
- convertedPath : path of the folder containing the converted files (this folder is created if necessary)"
- quality : JPEG quality level (1 to 100 - 75 by default)
- ow:1 : overwrite existing converted files (ow:0 by default) - Be carefull with this option if your original files are JPEG files !
- recurs:1 : process recursively the subfolders (recurs:0 by default)"
Example : ImageToJpeg.exe Photos\RAW Photos\JPEG 80 ow:1 recurs:1

This tool is not dedicated only to RAW files. It can be used to convert files of any format into JPEG since the codec is installed.

The executable and its sources can be downloaded here .

The source code

The code of this application operates by simply using the ConvertFiles method of the BitmapToJpegConverter Perspective class, passing it the command line arguments. Little subtlety : the Main method of the console application must be decorated with the STAThread attribute so that the thread model is compatible with WPF.

using System;
using Perspective.Core.Wpf.Imaging;
using System.Threading;
namespace ImageToJpeg
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            //...
            // Getting the command-line arguments
            //...
            BitmapToJpegConverter btjc = new BitmapToJpegConverter();
            btjc.QualityLevel = quality;
            btjc.Encoding += (sender, e) =>
            {
                Console.Write("{0}", e.OriginalFilename);
            };
            btjc.Encoded += (sender, e) =>
            {
                Console.WriteLine(" => {0} ", e.ConvertedFilename);
            };
            btjc.ConvertFiles(originalPath, convertedPath, recursive, overwrite);
            Console.WriteLine("Conversion done.");
            return;
        }
    }
}

The BitmapToPngConverter Perspective class can be used by the same principle to produce PNG files. The lowest level BitmapConverter class can produce other formats using the adequate WPF encoder.

About this article

Author : Olivier Dewit.

History :

  • septembre 7th, 2008 : 1st publication (Perspective version 1.0)