【发布时间】:2022-01-13 17:36:47
【问题描述】:
使用我提供的代码,我可以截取画布所在部分的屏幕截图。但是,我能够走上预定的道路。我想要做的是我想通过 savefiledialog 将图像保存到我想要的部分。我该怎么做。
private void btnKaydet_Click(object sender, RoutedEventArgs e)
{
UIElement element = cnvs 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 System.Windows.Point(0, 0), new System.Windows.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());
}
}
【问题讨论】:
标签: c# wpf screenshot