/// <summary> /// UI 打印 /// </summary> /// <param name="description">打印的文档名称</param> /// <param name="ele">要打印的UI</param> public static void ChartPrint(FrameworkElement ele, string description) { PrintDialog pd = new PrintDialog(); bool result = pd.ShowDialog().GetValueOrDefault(); if (result) { RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)ele.ActualWidth * 4, (int)ele.ActualHeight * 4, 96 * 4, 96 * 4, PixelFormats.Pbgra32); renderBitmap.Render(ele); //使用 RenderTargetBitmap 保存图片. // 使用图片作为载体显示UI. Image image = new Image(); image.Width = pd.PrintableAreaWidth - 45; image.Height = pd.PrintableAreaHeight - 45; image.StretchDirection = StretchDirection.DownOnly; image.Stretch = Stretch.Uniform; image.Source = renderBitmap; // 设置图片位置并显示. System.Windows.Window w = new System.Windows.Window(); Canvas cav = new Canvas(); Canvas.SetTop(image, 20); Canvas.SetLeft(image, 20); cav.Children.Add(image); w.Content = cav; w.Left = -10000; w.Top = -10000; w.Show(); w.Close(); pd.PrintVisual(image, description); // 打印图片. } }