I have this code:
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(525, 50, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(/* controlName */);
PngBitmapEncoder pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
using (Stream fileStream = File.Create(Environment.CurrentDirectory)
{
pngImage.Save(fileStream);
}
It supposed to take a control from my XAML and create a "screenshot" of it and then save it to an image file. But no matter what directory I try to pass to the File.Create
method, I get a System.UnauthorizedAccessException
.
How to fix it? Thanks.
Note: I have tried to run Visual Studio as an administrator, didn't work.