Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
{
    /// <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 = 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;

                    //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());
                }
            }
    }
}
Posted
Updated 19-Apr-19 3:46am

Yeah, on Windows Vista, or maybe 8, and up, the root directory of C:\ is ReadOnly to normal users. Users can create folders in the root of C: but not files.

You're going to have to put your screenshot file somewhere else more appropriate.

You never said anything about which other paths you tried so it's impossible to comment on those.
 
Share this answer
 
v2
Comments
tool__ 19-Apr-19 12:34pm    
Thanks guys for the replies, many locations seems to work, like the desktop. It maybe not appropriate for serious applications to write in the location of the executable, but in my case seemes the best solution. My program has no installation, it's just an .exe file and its meant to be running from the desktop...

Is there a way to express that? to write to wherever directory the executable is in... in any machine?
The root directory of your boot drive is protected, so no user can change it easily - this is an anti virus / malware feature of all Windows OSes since Vista and is unlikely to change. In fact, file access to the root of any drive is not recommended, and you can expect that to get more restrictive, not less.

See here: Where should I store my data?[^] for some better ideas.
 
Share this answer
 
v2
Comments
tool__ 19-Apr-19 10:46am    
Thanks guys for the replies, many locations seems to work, like the desktop. It maybe not appropriate for serious applications to write in the location of the executable, but in my case seemes the best solution. My program has no installation, it's just an .exe file and its meant to be running from the desktop...

Is there a way to express that? to write to wherever directory the executable is in... in any machine?
tool__ 19-Apr-19 11:02am    
Thanks guys for the replies, many locations seems to work, like the desktop. It maybe not appropriate for serious applications to write in the location of the executable, but in my case seems the best solution. My program has no installation, it's just an .exe file and its meant to be running from the desktop...

Is there a way to express that? to write to wherever directory the executable is in... in any machine?
tool__ 19-Apr-19 11:41am    
Thanks guys for the replies, many locations seems to work, like the desktop. It maybe not appropriate for serious applications to write in the location of the executable, but in my case seems the best solution. My program has no installation, it's just an .exe file and its meant to be running from the desktop...

Is there a way to express that? to write to wherever directory the executable is in... in any machine?

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