miércoles, 14 de agosto de 2013

Guardar un Grid como Imagen en WPF

Primero necesiamos agregar la calse Model con el siguiente Codigo:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.IO;

namespace WpfApplication9
{

public enum ImageFormat
{
        JPG, BMP, PNG, GIF, TIF
}
public class Model
{
          private ObservableCollection<BitmapSource> imagecollection;
public ObservableCollection<BitmapSource> ImageCollection
{
        get
        {
                this.imagecollection = this.imagecollection ?? new ObservableCollection<BitmapSource>();
                return this.imagecollection;
        }
}

public RenderTargetBitmap RenderVisaulToBitmap(Visual vsual, int width, int height)
{
        RenderTargetBitmap rtb = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Default);
        rtb.Render(vsual);
        BitmapSource bsource = rtb;
        this.ImageCollection.Add(bsource);
        return rtb;
}

public MemoryStream GenerateImage(Visual vsual, int widhth, int height, ImageFormat format)
{
        BitmapEncoder encoder = null;
        switch (format)
        {
                case ImageFormat.JPG :
                        encoder = new JpegBitmapEncoder();
                        break;
                case ImageFormat.PNG:
                        encoder = new PngBitmapEncoder();
                        break;
                case ImageFormat.BMP:
                        encoder = new BmpBitmapEncoder();
                        break;
                case ImageFormat.GIF:
                        encoder = new GifBitmapEncoder();
                        break;
                case ImageFormat.TIF:
                        encoder = new TiffBitmapEncoder();
                        break;
        }
        if (encoder == null) return null;

        RenderTargetBitmap rtb = this.RenderVisaulToBitmap(vsual, widhth, height);
        MemoryStream file = new MemoryStream();
        encoder.Frames.Add(BitmapFrame.Create(rtb));
        encoder.Save(file);

        return file;
    }
}
}
Luego se agrega en el Windows.Resource de l Xaml
<
Window.Resources>
            <local:Model x:Key="Foto" />
</Window.Resources>

Instanciamos la case y lo referimos al recuros del xaml

 private Model DataModel
{
        get { return this.Resources["Foto"] as Model; }
}

ahora usamos el evento del boton que renderisará el grid  a una imagen y listo

private void Button_Click_1(object sender, RoutedEventArgs e)
{
        BitmapEncoder encoder = new JpegBitmapEncoder();
        //ImgFinal --> Grid aconvertir
        RenderTargetBitmap rtb = this.DataModel.RenderVisaulToBitmap(ImgFinal, (int)ImgFinal.ActualWidth, (int)ImgFinal.ActualHeight);
        MemoryStream file = new MemoryStream();
        encoder.Frames.Add(BitmapFrame.Create(rtb));
        encoder.Save(file);

        if (file != null)
        {
                SaveFileDialog fdlg = new SaveFileDialog
                {
                        DefaultExt = "jpg",
                        Title = "Choose filename and location",
                        Filter = "*Jpeg files|.jpg|Bmp Files|*.bmp|PNG Files|*.png|Tiff Files|*.tif|Gif Files|*.gif"
                };
                bool? result = fdlg.ShowDialog();
                if (result.HasValue && result.Value)
                {
                        using (FileStream fstream = File.OpenWrite(fdlg.FileName))
                        {
                                file.WriteTo(fstream);
                                fstream.Flush();
                                fstream.Close();
                        }
                }
        }
}

No hay comentarios:

Publicar un comentario