using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; namespace DTS.Common.Utils { /// /// this class holds PNG image functions /// public static class PNGImageUtil { /// /// renders the given element as a png to the given filepath /// /// /// public static void SaveImage(FrameworkElement view, string fileName) { var size = new Size(view.ActualWidth, view.ActualHeight); var rtb = new RenderTargetBitmap((int) size.Width, (int) size.Height, 96, 96, PixelFormats.Pbgra32); var visual = new DrawingVisual(); using (var context = visual.RenderOpen()) { context.DrawRectangle(new VisualBrush(view), null, new Rect(new Point(), size)); context.Close(); } rtb.Render(visual); var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); using (var stream = new System.IO.FileStream(fileName, System.IO.FileMode.CreateNew)) { encoder.Save(stream); } } } }