Title: 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();
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 the example to experiment with it and to see additional details.
|