生成WPF窗口的屏幕截图 [英] Generating a screenshot of a WPF window

查看:2155
本文介绍了生成WPF窗口的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的WinForms,我们可以使用DrawToBitmap。是否有一个类似的方法来这WPF?

In winforms, we can use DrawToBitmap. Is there a similar method to this in WPF?

推荐答案

你试过 RenderTargetBitmap https://msdn.microsoft.com/en-us/library/ system.windows.media.imaging.rendertargetbitmap.aspx

有周围使用了一些截屏的方法是,像这样的拍摄的从这里

There are a few "screenshot" methods around that use that, like this one taken 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);
        }
    }

这篇关于生成WPF窗口的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆