Saving images (.bmp, .png, etc) in WPF/Silverlight

I’ve recently added a new feature to Live Geometry that allows users to save the current drawing as a bitmap or a .png file. Just push the save button and pick the desired image format in the Save dialog:


Fortunately, both WPF and Silverlight support saving full visual contents of any visual into a file on disk. However the approach is somewhat different.

Saving images in WPF

WPF can save any Visual to an image and it supports several formats out of the box via a concept of Encoders. Here’s a sample for .bmp and .png:

void SaveToBmp(FrameworkElement visual, string fileName)
    var encoder = new BmpBitmapEncoder();
    SaveUsingEncoder(visual, fileName, encoder);

void SaveToPng(FrameworkElement visual, string fileName)
    var encoder = new PngBitmapEncoder();
    SaveUsingEncoder(visual, fileName, encoder);

void SaveUsingEncoder(FrameworkElement visual, string fileName, BitmapEncoder encoder)
    RenderTargetBitmap bitmap = new RenderTargetBitmap(
    BitmapFrame frame = BitmapFrame.Create(bitmap);

    using (var stream = File.Create(fileName))

These types are all in System.Windows.Media.Imaging.

Saving images in Silverlight 3

In Silverlight, the encoders don’t come as part of the Silverlight runtime – but fortunately there is a project on CodePlex called ImageTools ( that provides necessary support. You will need to download the following binaries and add them as references to your Silverlight project:

  • ICSharpCode.SharpZipLib.Silverlight
  • ImageTools
  • ImageTools.IO
  • ImageTools.IO.Png (only if you want .png support)
  • ImageTools.IO.Bmp (only if you want .bmp support)
  • ImageTools.Utils

After that, you can call the ToImage() extension method on any Canvas:

void SaveAsPng(Canvas canvas, SaveFileDialog dialog)
    SaveToImage(canvas, dialog, new PngEncoder());

void SaveAsBmp(Canvas canvas, SaveFileDialog dialog)
    SaveToImage(canvas, dialog, new BmpEncoder());

void SaveToImage(Canvas canvas, SaveFileDialog dialog, IImageEncoder encoder)
    using (var stream = dialog.OpenFile())
        var image = canvas.ToImage();
        encoder.Encode(image, stream);

Since you can’t write to disk directly in Silverlight, you can pass a SaveFileDialog and use its stream, or you can obtain a stream elsewhere and pass that. The ToImage() extension method does the dirty work that we had to do ourselves in WPF.

Big thanks to for their awesome library and encoders!

Comments (10)
  1. Greg says:

    Please document the .net image encoder properties.  The current API does not document the supported forms of TIFF encoding, jpeg encoding or PNG encoding.  

    Tiff (group 3, group 4, packbits, lzw, horizontal tile size, verticle tile size, background color, image bounding box)

  2. Quang says:


    do not contain defindion ToImage();

    Please help me?

  3. Waqar Ahmad says:


    do not contain defindion ToImage();

    I m having same issue any help ?

  4. Kevin Jobson says:

    for those with the problems with the extension method. Add this to the top of your page:

    using ImageTools;

  5. murugan says:


    i saved image only what i am seen in the View, if my Image is big and scrol bars came that part is not saved.. also more than300*3000 also not able to create image .. if this thing solved it is perfect to use. Thanks

  6. murugan says:

    My Email id is  pls replay my comment if u need more info  pls ask me

  7. Devendra Gadwal says:

    How we can save the image in isolated storage and display in image control with silverlight .

    Pleas help me .

    I have an issue I can show the ashx image in silverlight apps but when its xap use in windows .vb apps image is not going to work its not showing  what is issue I could not understand how can I short out this one please hwlp me

  8. Devendra Gadwal says:

    How can I download the image like "http://../../Handler.ashx?winnerId=27" and save in local folder in silverlight ,

    I have used so many way but I couldn't get any right solution so that this image display in .net windows application.

  9. ChardLee says:

    if i have a Image instance, not files provided by OpenFileDialog, do you have solutions to save this Image instance to server drives.

  10. LeeCampbell says:

    Nice one. Cheers.

Comments are closed.

Skip to main content