Wednesday, July 27, 2016

How to capture screenshot of WPF application?

In WPF application you can easily capture screen of your WPF window. In this article, I will show you how to take screenshot of full content of window.

Sometimes you need to capture all content of your window in WPF application as screenshot. You can do it via Window screen capture (using Print Screen button) but your application has large content and has scrollbar so the content is not visible and not captured via Windows print screen. In such scenario you can use below sample code to capture WPF window with full content. See below code snippet.

<Window x:Class="ScreenCaptureDemo.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <ScrollViewer Name="scrollViewer">
                <RowDefinition Height="50" />
                <RowDefinition Height="450"/>
                <RowDefinition Height="50" />
                <ColumnDefinition />
            <Button Content="Capture Screen"
                Width="150" Height="50" Click="Button_Click"  />
            <TextBlock Text="Top Left Content"
            <TextBlock Text="Top Rigth Content"
            <TextBlock Text="Bottom Left Content"
            <TextBlock Text="Bottom Rigth Content"
                   HorizontalAlignment="Right" ></TextBlock>

public partial class MainWindow : Window
    public MainWindow()

    private void Button_Click(object sender, RoutedEventArgs e)
        //Set scrollviewer's Content property as UI element to capture full content
        UIElement element = scrollViewer.Content as UIElement;
        Uri path = new Uri(@"d:\temp\screenshot.png");
        CaptureScreen(element, path);
    public void CaptureScreen(UIElement source, Uri destination)
            double Height, renderHeight, Width, renderWidth;

            Height =  renderHeight = source.RenderSize.Height;
            Width =  renderWidth = source.RenderSize.Width;

            //Specification for target bitmap like width/height pixel etc.
            RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
            //creates Visual Brush of UIElement
            VisualBrush visualBrush = new VisualBrush(source);

            DrawingVisual drawingVisual = new DrawingVisual();
            using (DrawingContext drawingContext = drawingVisual.RenderOpen())
                //draws image of element
                drawingContext.DrawRectangle(visualBrush, null, new Rect(new Point(0, 0), new Point(Width, Height)));
            //renders image

            //PNG encoder for creating PNG file
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            using (FileStream stream = new FileStream(destination.LocalPath, FileMode.Create, FileAccess.Write))
        catch (Exception e)

Output –

Generated PNG File –

As you can see in above example, MainWindow has a scrollbar and has some more content which is not visible currently on window. When you click on Capture Screen button it will capture full content of MainWindow and shows full content in generated file as per above image.

I hope you liked this article. Please leave your feedback in comments below.

See Also –

No comments:

Post a Comment