Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I modified the code from this page:

Mitesh Sureja's Blog: How to capture screenshot of WPF application?[^]

Because it seems to do what I want. I replaced the button click handler with a key down, and I replaced the scrollViewer (which my program doesn't have ) with 'Grid' and I gave it the name 'gridx' as you see in the XAML. I also added on top the namespace: ''using System.IO;''

However in the line
''UIElement element = gridx.Content as UIElement;''
the word 'Content' is underlined red and it say:

'Grid' does not contain a definition for 'content' and no accessible extension method 'Content' accepting a first argument of type 'Grid' could be found. (are you missing a using directive or an assembly refereance?)

Can you guys tell me what i do wrong?

Thanks

What I have tried:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (linevertical.Visibility == Visibility.Hidden)
            { linevertical.Visibility = Visibility.Visible; }
            else { linevertical.Visibility = Visibility.Hidden; }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (lineo.Visibility == Visibility.Hidden)
            { lineo.Visibility = Visibility.Visible; }
            else { lineo.Visibility = Visibility.Hidden; }
        }

        private void Mainwindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.A)
            {
                lineo.Visibility = Visibility.Hidden;
                linevertical.Visibility = Visibility.Hidden;
            }
            if (e.Key == Key.S)
            {
                lineo.Visibility = Visibility.Visible;
                linevertical.Visibility = Visibility.Visible;

            }

            if (e.Key == Key.P)
            {
                //Set scrollviewer's Content property as UI element to capture full 
                content
                UIElement element = gridx.Content as UIElement;
                Uri path = new Uri(@"d:\temp\screenshot.png");
                CaptureScreen(element, path);
            }

        }
            public void CaptureScreen(UIElement source, Uri destination)
            {
                try
                {
                    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
                    renderTarget.Render(drawingVisual);

                    //PNG encoder for creating PNG file
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(renderTarget));
                    using (FileStream stream = new 
                    FileStream(destination.LocalPath, FileMode.Create, 
                    FileAccess.Write))
                    {
                        encoder.Save(stream);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }        
    }
}


XAML

<Grid Name="gridx" Margin="0,0,0,13">
Posted
Updated 18-Apr-19 23:26pm
v2

1 solution

The Grid control does not have a Content property: ScrollViewer Class (System.Windows.Controls) | Microsoft Docs[^] - that is part of the ScrollView control, and is inherited from ContentControl. Grid does not inherit directly or indirectly from ContentControl so it does not and will not have a Content property.
 
Share this answer
 
Comments
tool__ 19-Apr-19 5:53am    
Thank you, i give a name to Window x:Name="mainwindow"
and i use that instead, and the error is gone, the print screen still not working but i will make another thread for the new problem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900