Save WriteableBitmap images in multiple formats in WPF and C#


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();
            case ImageFormats.Gif:
                encoder = new GifBitmapEncoder();
            case ImageFormats.Jpg:
                encoder = new JpegBitmapEncoder();
            case ImageFormats.Png:
                encoder = new PngBitmapEncoder();
            case ImageFormats.Tif:
                encoder = new TiffBitmapEncoder();
            case ImageFormats.Wmp:
                encoder = new WmpBitmapEncoder();

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);
        case ".gif":
            wbitmap.Save(filename, ImageFormats.Gif);
        case ".jpg":
        case ".jpeg":
            wbitmap.Save(filename, ImageFormats.Jpg);
        case ".png":
            wbitmap.Save(filename, ImageFormats.Png);
        case ".tif":
        case ".tiff":
            wbitmap.Save(filename, ImageFormats.Tif);
        case ".wmp":
            wbitmap.Save(filename, ImageFormats.Wmp);

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.

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.

1 Response to Save WriteableBitmap images in multiple formats in WPF and C#

  1. Angel says:

    Thanks Rod for the post, I already wanted articles about WPF. I think that for Windows is the best technology.

Comments are closed.