任何人都知道如何可靠地拍摄WPF窗口的快照? PrintWindow api适用于“标准”win32窗口,但由于WPF使用DirectX,因此PrintWindow无法捕获图像。我认为需要为与窗口关联的DirectX对象获取前缓冲区,但我不知道该怎么做。
谢谢!
答案 0 :(得分:4)
我不确定这是不是你的意思,我不确定我是否可以链接到我的博客,但this是否有用?它基本上使用RenderTargetBitmap来生成JPG。您可以使用它来“截屏”整个窗口,然后打印出来。
如果这违反规则,有人可以随意删除:)
答案 1 :(得分:1)
此方法应该可以帮助您打印整个WPF / XAML窗口
private void PrintWindow(PrintDialog pdPrint,
System.Windows.Window wWin,
string sTitle,
System.Windows.Thickness? thMargin)
{
Grid drawing_area = new Grid();
drawing_area.Width = pdPrint.PrintableAreaWidth;
drawing_area.Height = pdPrint.PrintableAreaHeight;
Viewbox view_box = new Viewbox();
drawing_area.Children.Add(view_box);
view_box.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
view_box.VerticalAlignment = System.Windows.VerticalAlignment.Center;
if (thMargin == null)
{
view_box.Stretch = System.Windows.Media.Stretch.None;
}
else
{
view_box.Margin = thMargin.Value;
view_box.Stretch = System.Windows.Media.Stretch.Uniform;
}
VisualBrush vis_br = new VisualBrush(wWin);
System.Windows.Shapes.Rectangle win_rect = new System.Windows.Shapes.Rectangle();
view_box.Child = win_rect;
win_rect.Width = wWin.Width;
win_rect.Height = wWin.Height;
win_rect.Fill = vis_br;
win_rect.Stroke = System.Windows.Media.Brushes.Black;
win_rect.BitmapEffect = new System.Windows.Media.Effects.DropShadowBitmapEffect();
// Arrange to produce output.
Rect rect = new Rect(0, 0, pdPrint.PrintableAreaWidth, pdPrint.PrintableAreaHeight);
drawing_area.Arrange(rect);
// Print it.
pdPrint.PrintVisual(drawing_area, sTitle);
}
关心肖恩坎贝尔
答案 2 :(得分:0)
您可以使用PrintDialog.PrintVisual()方法。
MSDN链接:http://msdn.microsoft.com/en-us/library/system.windows.controls.printdialog.printvisual.aspx