The program below creates a snapshot with the content of the main window of the application itself. However the quality of the produced picture is not equivalent to the print screen program of windows 10, which produces the desired result.
Here is a snapshot of the program running, taken with the print screen program of windows 10, zoomed in:
https://ibb.co/wz4pb4d
And here is the snapshot that the program below is producing, zoomed in:
https://ibb.co/DLsNb8X
Is there something we can try to improve the quality of the snapshot that this program produse?
I tried Bitmap Encoder but is the same result , just without transparency, (Don't need to have transparency) also tried some other Pixel Formats but I get errors, only Pbgra32 seems to work as the program is.
Thanks in advance
What I have tried:
<pre> if (e.Key == Key.P)
{
FrameworkElement element = UxVisual as FrameworkElement;
Uri path = new Uri(@"C:\Users\4gry\Desktop\screenshot.png");
CaptureScreen(element, path);
}
}
public void CaptureScreen(FrameworkElement source, Uri destination)
{
RenderOptions.SetEdgeMode(source, EdgeMode.Aliased);
try
{
double Height, ActualHeight, Width, ActualWidth;
Height = ActualHeight = source.RenderSize.Height;
Width = ActualWidth = source.RenderSize.Width;
RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)ActualWidth, (int)ActualHeight, 96, 96, PixelFormats.Pbgra32);
VisualBrush visualBrush = new VisualBrush(source);
DrawingVisual drawingVisual = new DrawingVisual();
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
drawingContext.DrawRectangle(visualBrush, null, new Rect(source.RenderSize));
}
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
<Window x:Name="mainwindow" x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="mainwindow" Height="397.265" Width="603.147" Icon="images2/Untitled-1.gif" ResizeMode="CanMinimize" WindowStartupLocation="Manual" AutomationProperties.Name="Grid" IsTabStop="True" KeyDown="Mainwindow_KeyDown" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0">
<Border x:Name="UxVisual" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="367" Margin="10,0,0,0" VerticalAlignment="Top" Width="583">
<Grid Margin="0,0,0,13">
<Image Stretch="None" Source="images2/screenshot copy.gif" IsEnabled="False" Margin="0,0,-28,0" HorizontalAlignment="Left" Width="625" Height="347" VerticalAlignment="Top"/>
<Image x:Name="linevertical" HorizontalAlignment="Left" Height="343" Margin="108,76,-21,0" VerticalAlignment="Top" Width="705" Source="images2/Untitled-1.gif" Stretch="None" Visibility="Hidden" RenderTransformOrigin="0.46,0.52"/>
<Image x:Name="lineo" HorizontalAlignment="Left" Height="171" Margin="173,76,0,0" VerticalAlignment="Top" Width="193" Source="images2/Untitled-3.gif" Stretch="None" Visibility="visible"/>
<Image HorizontalAlignment="Left" Height="100" Margin="10,16,0,0" VerticalAlignment="Top" Width="189" Source="images2/Untitled-1.gif" Stretch="None" Visibility="hidden"/>
<Image HorizontalAlignment="Left" Height="100" Margin="73,10,0,0" VerticalAlignment="Top" Width="100" Source="images2/Untitled-3.gif" Stretch="None" Visibility="visible"/>
<Button HorizontalAlignment="Left" Margin="173,61,0,0" VerticalAlignment="Top" Width="225" Height="121" Opacity="0" Click="Button_Click"/>
<Button Content="" HorizontalAlignment="Left" Margin="464,247,0,0" VerticalAlignment="Top" Width="194" Height="144" Opacity="0" Click="Button_Click_1"/>
<Image HorizontalAlignment="Left" Height="100" Margin="428,-11,-40,0" VerticalAlignment="Top" Width="206" Stretch="None" Source="images2/Untitled-1.gif"/>
</Grid>
</Border>
</Window>