c# - 生成 WPF 窗口的屏幕截图

标签 c# wpf

在winforms中,我们可以使用DrawToBitmap。 WPF中有类似的方法吗?

最佳答案

您是否尝试过RenderTargetBitmaphttps://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/

相关文章:

wpf - 编辑标准 WPF Aero 主题

c# - 与 ICommand 的 WPF 绑定(bind)错误

.net - 第 3 方组件许可在 WPF 中如何工作?

c# - 获取文件列表

c# - 如何在自定义 wpf 控件上绑定(bind)数据网格列的可见性?

c# - 通过 WIF 在 MVC 应用程序之间共享身份验证

c# - OpenCV Marker Z 轴变换不正确

wpf - 发生拖动操作时,如何在 XAML 中激活 WPF 触发器?

c# - GetWebResponse 在 Windows 7 上非常慢

c# - 最简单的 C# 开发免费设置