The program below is taking a snapshot of the main window by pressing the key ''p''. There are no underlined red errors in the code.
when the program is runing and I press ''p'' it shows the error in the image:
image — imgbb.com[
^]
In greek it say access is denied to the path...
whatever path I tried here:
Uri path = new Uri(@"c:\screenshot.png");
it always show the same error.
Any ideas what may causes this or what is missing from the code?
Thanks in advance
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)
{
UIElement element = mainwindow.Content as UIElement;
Uri path = new Uri(@"c:\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());
}
}
}
}