截取 wpf 弹出窗口的屏幕截图 [英] Take screenshot of wpf popup window

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

问题描述

我尝试截取一个用 WPF 编写的应用程序,但应用程序没有被捕获,我必须使用特殊工具截取屏幕截图吗?

I try to take a screenshot of an application writen in WPF and the application is not captured, must I use a special tool to take the screenshot?

推荐答案

你可以使用RenderTargetBitmap 从您的 WPF 控件生成图像.

You can use RenderTargetBitmap to generate an image from your WPF control.

    public const int IMAGE_DPI = 96;

    public Image GenerateImage(T control)
        where T : Control, new()
    {
        Size size = RetrieveDesiredSize(control);

        Rect rect = new Rect(0, 0, size.Width, size.Height);

        RenderTargetBitmap rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, IMAGE_DPI, IMAGE_DPI, PixelFormats.Pbgra32);

        control.Arrange(rect); //Let the control arrange itself inside your Rectangle
        rtb.Render(control); //Render the control on the RenderTargetBitmap

        //Now encode and convert to a gdi+ Image object
        PngBitmapEncoder png = new PngBitmapEncoder();
        png.Frames.Add(BitmapFrame.Create(rtb));
        using (MemoryStream stream = new MemoryStream())
        {
            png.Save(stream);
            return Image.FromStream(stream);
        }
    }

    private Size RetrieveDesiredSize(T control)
    {
        if (Equals(control.Width, double.NaN) || Equals(control.Height, double.NaN))
        {
            //Make sure the control has measured first:
            control.Measure(new Size(double.MaxValue, double.MaxValue));

            return control.DesiredSize;
        }

        return new Size(control.Width, control.Height);
    }

请注意,这将生成一个 PNG 图像;)如果您希望将其存储为 JPEG,我建议您使用另一个编码器 :)

Note that this will generate a PNG image ;) If you wish to store it as a JPEG, I suggest you use another encoder :)

Image image = GenerateImage(gridControl);
image.Save("mygrid.png");

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

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