반응형
public static void CaptureScreen(UIElement source, string saveImageFilePath)
{
Uri destination = new Uri(saveImageFilePath);
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
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (FileStream stream = new FileStream(destination.LocalPath, FileMode.Create, FileAccess.Write))
{
encoder.Save(stream);
}
}
반응형
'[====== Development ======] > C#' 카테고리의 다른 글
[C#] exe 파일로 MainWindow의 Handle값 구하기 (0) | 2022.03.11 |
---|---|
[C#] 폴더 안의 모든 파일및 폴더 복사하기 (0) | 2022.03.07 |
[WPF] Read BitmapImage From File (0) | 2022.03.07 |
[C#] Window Registry Key, Value Set/Get/Delete (0) | 2022.03.07 |
[C#] IniFile Helper Class (0) | 2022.03.07 |