1

I am using following code to get source for image in wpf (image is of type System.Windows.Controls.Image). I want to store this image to disk with it original extension, is it possible?

System.drawing.image provides save method is there something like that for System.windows.controls.image?

Image image=new Image();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(openFileDlg.FileName);
bitmap.EndInit();               
image.Source = bitmap;                
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
user913359
  • 660
  • 7
  • 14
  • 1
    See [BitmapEncoder](http://msdn.microsoft.com/en-us/library/ms619208) (and derived classes) for how to save an image to a (file) stream. MSDN provides plenty of code samples. – Clemens Jun 26 '12 at 07:51

1 Answers1

2

Take a look at RenderTargetBitmap and BitmapEncoder classes.

The following utility class allows you to save your Image as raster graphics image, according to the chosen format:

public static class VisualToImg
{

    static VisualToImg()
    {
        DPI = 96;
        PixelFormat = PixelFormats.Default;
    }

    public enum eImageFormat
    { Bitmap, Png, Gif, Jpeg, Tiff, Wmp }

    static public bool SaveAs(FrameworkElement element, String filePath,
        eImageFormat imageFormat)
    {
        switch (imageFormat)
        {
            case eImageFormat.Bitmap:
                return SaveUsingEncoder(element, filePath, new BmpBitmapEncoder());
            case eImageFormat.Gif:
                return SaveUsingEncoder(element, filePath, new GifBitmapEncoder());
            case eImageFormat.Jpeg:
                return SaveUsingEncoder(element, filePath, new JpegBitmapEncoder());
            case eImageFormat.Png:
                return SaveUsingEncoder(element, filePath, new PngBitmapEncoder());
            case eImageFormat.Tiff:
                return SaveUsingEncoder(element, filePath, new TiffBitmapEncoder());
            case eImageFormat.Wmp:
                return SaveUsingEncoder(element, filePath, new WmpBitmapEncoder());
        }

        return false;
    }

    static public double DPI
    { get; set; }

    static public PixelFormat PixelFormat
    { get; set; }

    static private bool SaveUsingEncoder(FrameworkElement visual, string filePath,
        BitmapEncoder encoder)
    {
        try
        {
            RenderTargetBitmap bmp = new RenderTargetBitmap((int)visual.ActualWidth,
                (int)visual.ActualHeight, DPI, DPI, PixelFormat);

            bmp.Render(visual);
            encoder.Frames.Add(BitmapFrame.Create(bmp));

            using (var stream = File.Create(filePath))
            { encoder.Save(stream); }

            return true;
        }
        catch (Exception)
        { return false; }
    }

}

And you can use it to save your Image, or any other FrameworkElement:

VisualToImg.SaveAs(image, "image1.png", VisualToImg.eImageFormat.Png);
VisualToImg.SaveAs(image, "image2.bmp", VisualToImg.eImageFormat.Bitmap);
VisualToImg.SaveAs(image, "image3.jpeg", VisualToImg.eImageFormat.Jpeg);
gliderkite
  • 8,828
  • 6
  • 44
  • 80