在winforms中,我们可以使用DrawToBitmap。 WPF中有类似的方法吗?
最佳答案
您是否尝试过RenderTargetBitmap
? https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx
有一些使用它的“屏幕截图”方法,比如这个 from here :
public static void CreateBitmapFromVisual(Visual target, string fileName)
{
if (target == null || string.IsNullOrEmpty(fileName))
{
return;
}
Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
RenderTargetBitmap renderTarget = new RenderTargetBitmap((Int32)bounds.Width, (Int32)bounds.Height, 96, 96, PixelFormats.Pbgra32);
DrawingVisual visual = new DrawingVisual();
using (DrawingContext context = visual.RenderOpen())
{
VisualBrush visualBrush = new VisualBrush(target);
context.DrawRectangle(visualBrush, null, new Rect(new Point(), bounds.Size));
}
renderTarget.Render(visual);
PngBitmapEncoder bitmapEncoder = new PngBitmapEncoder();
bitmapEncoder.Frames.Add(BitmapFrame.Create(renderTarget));
using (Stream stm = File.Create(fileName))
{
bitmapEncoder.Save(stm);
}
}
关于c# - 生成 WPF 窗口的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5616906/