public static RenderTargetBitmap ToRenderTargetBitmap(this System.Windows.UIElement element, Size size, PixelFormat pixelFormat) { var result = new RenderTargetBitmap((int)size.Width, (int)size.Height, 96, 96, pixelFormat); element.Measure(size); element.Arrange(new System.Windows.Rect(size)); result.Render(element); return(result); }
public static void SaveImage(this System.Windows.UIElement element, Size size, PixelFormat pixelFormat, string fileName) { using (var stream = File.OpenWrite(fileName)) { element.Measure(size); element.Arrange(new System.Windows.Rect(size)); var renderTargetBitmap = element.ToRenderTargetBitmap(size, pixelFormat); var encoder = GetEncoder(fileName); encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap)); encoder.Save(stream); } }