Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrintWindow WPF / DirectX

Anyone know of 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 fails to capture an image. I think that one would need to grab the front buffer for the DirectX object associated with the window, but I am not sure how to do that.

Thanks!

like image 584
Maurice Flanagan Avatar asked Jan 05 '09 16:01

Maurice Flanagan


2 Answers

I'm not sure if this is what you mean, and I'm not sure I'm allowed to link to my blog or not, but is this any use? It basically uses a RenderTargetBitmap to generate a JPG. You can use it to "screenshot" an entire window then print that.

If this is against the rules, someone feel free to delete :)

like image 140
Steven Robbins Avatar answered Nov 07 '22 19:11

Steven Robbins


This Method should help you print the entire 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);

    }

Regards Sean Campbell

like image 36
Sean Campbell Avatar answered Nov 07 '22 18:11

Sean Campbell