PrintWindow WPF / DirectX

Does anyone know a way to reliably take a snapshot of a WPF window? The PrintWindow api works well for "standard" win32 windows, but since WPF uses DirectX, PrintWindow cannot capture the image. I think one would need to grab the front buffer for the DirectX object associated with the window, but I'm not sure how.

Thank!

+4


source to share


3 answers


I'm not sure if this is what you mean and I'm not sure if I am allowed to link to my blog or not, but is this any use? It mainly uses RenderTargetBitmap to generate JPGs. You can use it to "screenshot" the entire window and then print it out.



If it is against the rules, someone can delete :)

+4


source


This method should help you to print the whole WPF / XAML window

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

    }

      



Best regards, Sean Campbell

+1


source


0


source







All Articles