Save WriteableBitmap images in multiple formats in WPF and C#

[WriteableBitmap]

The post Easily save a WriteableBitmap in WPF and C# describes an extension method that makes it easy to save a WriteableBitmap object into a PNG file, but what if you want to save the image in some other format such as GIF or JPG? This example defines extension methods to allow you to do that.


The static WriteableBitmapExtensions class begins with the following enumeration that lists the available image file types.

// The available types.
public enum ImageFormats
{
    Bmp, Gif, Jpg, Png, Tif, Wmp
}

The following extension method saves a WriteableBitmap object into a file with a particular format.

// Save the WriteableBitmap into a graphic file of a given type.
public static void Save(this WriteableBitmap wbitmap,
    string filename, ImageFormats image_format)
{
    // Save the bitmap into a file.
    using (FileStream stream =
        new FileStream(filename, FileMode.Create))
    {
        BitmapEncoder encoder = null;
        switch (image_format)
        {
            case ImageFormats.Bmp:
                encoder = new BmpBitmapEncoder();
                break;
            case ImageFormats.Gif:
                encoder = new GifBitmapEncoder();
                break;
            case ImageFormats.Jpg:
                encoder = new JpegBitmapEncoder();
                break;
            case ImageFormats.Png:
                encoder = new PngBitmapEncoder();
                break;
            case ImageFormats.Tif:
                encoder = new TiffBitmapEncoder();
                break;
            case ImageFormats.Wmp:
                encoder = new WmpBitmapEncoder();
                break;
        }
        encoder.Frames.Add(BitmapFrame.Create(wbitmap));
        encoder.Save(stream);
    }
}

The method first creates a stream for the desired output file. It then declares a BitmapEncoder object. It then uses a switch statement to set that object to an instance of the appropriate encoder class for the desired image file type.

The method then creates a BitmapFrame for the WriteableBitmap and adds it to the encoder’s Frames collection. It finishes by saving the encoder’s data into the stream.

That method lets you save a WriteableBitmap into a file with a given image format, but normally the file’s extension matches the format. For example, you could (but probably wouldn’t want to) use that method to save an image in GIF format into the file names incompatible.png.

The following extension method saves a WriteableBitmap into a file with the appropriate format.

// Save the WriteableBitmap into a PNG file.
public static void Save(this WriteableBitmap wbitmap,
    string filename)
{
    FileInfo file_info = new FileInfo(filename);
    switch (file_info.Extension.ToLower())
    {
        case ".bmp":
            wbitmap.Save(filename, ImageFormats.Bmp);
            break;
        case ".gif":
            wbitmap.Save(filename, ImageFormats.Gif);
            break;
        case ".jpg":
        case ".jpeg":
            wbitmap.Save(filename, ImageFormats.Jpg);
            break;
        case ".png":
            wbitmap.Save(filename, ImageFormats.Png);
            break;
        case ".tif":
        case ".tiff":
            wbitmap.Save(filename, ImageFormats.Tif);
            break;
        case ".wmp":
            wbitmap.Save(filename, ImageFormats.Wmp);
            break;
    }
}

The method creates a FileInfo for the output file. It then uses a switch statement on the file’s extension to see what file format is appropriate. It then calls the preceding extension method to save the WriteableBitmap into the file with the correct format.

This second extension method makes saving WriteableBitmap objects really simple. The example program uses the following statements to save its image into six different files with various formats.

// Save the bitmap into different kinds of files.
wbitmap.Save("ColorSamples.bmp");
wbitmap.Save("ColorSamples.gif");
wbitmap.Save("ColorSamples.jpg");
wbitmap.Save("ColorSamples.png");
wbitmap.Save("ColorSamples.tif");
wbitmap.Save("ColorSamples.wmp");

Each of the statements saves the image with a different file format in the program’s current directory.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in extension methods, graphics, image processing, wpf, XAML and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.