WPF保存控件到图片
/// <summary> /// 保存控件到图片 /// </summary> /// <param name="element">控件对象</param> /// <param name="fileName">文件名</param> /// <param name="dpi">DPI</param> public static void SaveControlToPng(FrameworkElement element, string fileName, int dpi = 96) { var size = new System.Windows.Size(element.ActualWidth, element.ActualHeight); element.Measure(size); element.Arrange(new Rect(new System.Windows.Point(0, 0), size)); var bitmap = new RenderTargetBitmap( (int) size.Width, //width (int) size.Height, //height dpi, //dpi x dpi, //dpi y PixelFormats.Pbgra32 // pixelformat ); bitmap.Render(element); var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bitmap)); using (var stream = File.Create(fileName)) { encoder.Save(stream); } }