Monday, 21 November 2011

How to get a Bitmap from a Visual

 
public void SaveImage(Visual visual, int width, int height, string filePath)
{
    RenderTargetBitmap bitmap = new RenderTargetBitmap(width, height, 96, 96,
                                                 PixelFormats.Pbgra32);
    bitmap.Render(visual);
 
    PngBitmapEncoder image = new PngBitmapEncoder();
    image.Frames.Add(BitmapFrame.Create(bitmap));
    using (Stream fs = File.Create(filePath))
    {
        image.Save(fs);
    }
} 
 

No comments:

Post a Comment