打印窗口 WPF / DirectX

问题描述 投票:0回答:3

有人知道一种可靠地拍摄 WPF 窗口快照的方法吗? PrintWindow API 适用于“标准”win32 窗口,但由于 WPF 使用 DirectX,PrintWindow 无法捕获图像。我认为需要获取与窗口关联的 DirectX 对象的前缓冲区,但我不知道如何做到这一点。

wpf winapi directx
3个回答
4
投票

我不确定这是否是你的意思,我也不确定我是否被允许链接到我的博客,但是有什么用吗?它基本上使用 RenderTargetBitmap 来生成 JPG。您可以使用它对整个窗口进行“屏幕截图”,然后将其打印出来。


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);

    }

© www.soinside.com 2019 - 2024. All rights reserved.