Программа ниже создает снимок с содержимым главного окна самого приложения. Однако качество получаемого изображения не эквивалентно программе печати экрана Windows 10, которая дает желаемый результат.
Вот снимок запущенной программы, сделанный программой печати экрана windows 10, увеличенный:
https://ibb.co/wz4pb4d
А вот снимок, который создает приведенная ниже программа, увеличенный:
https://ibb.co/DLsNb8X
Можем ли мы попытаться улучшить качество снимка, создаваемого этой программой?
Я попробовал Bitmap Encoder, но это тот же результат, только без прозрачности, (не нужно иметь прозрачность) также пробовал некоторые другие форматы пикселей, но я получаю ошибки, только Pbgra32, кажется, работает как программа.
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:\Users\4gry\Desktop\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());
}
}
}
}