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
{
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)
{
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;
RenderTargetBitmap renderTarget = new
RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96,
PixelFormats.Pbgra32);
VisualBrush visualBrush = new VisualBrush(source);
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext =
drawingVisual.RenderOpen())
{
drawingContext.DrawRectangle(visualBrush, null, new
Rect(new Point(0, 0), new Point(Width, Height)));
}
renderTarget.Render(drawingVisual);
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">