Take screenshot of wpf popup window

2019-04-12 15:02发布

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?

3条回答
可以哭但决不认输i
2楼-- · 2019-04-12 15:35

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);
    }

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");
查看更多
神经病院院长
3楼-- · 2019-04-12 15:39

I'm having the same issue, I need to take screenshots to document my tests but can't seem to get there.

The Window in question is a Borderless modal Window with rounded corners / transparency allowed. Here's my report:

  • HP Quality Center's Screenshot tool doesn't recognize it as a window and thus takes its screenshots as if the window weren't there.
  • SnagIt utilizes a keycombo to enter the capture mode. Once stroke is hit, the popup vanishes. It reappears after the capture has ended.
  • Standard Windows capture works OK (Alt + Prt Scr) and captures the window as it was intended to be captured.

Next thing I tried was capturing the window with an opened dropdownlist. None of the approaches mentioned above seems to work (the last approach captures the Window the same way it did before, without the open dropdown).

As far as I have understood all the talk correctly, the only thing you can do is implement this into the applications...

查看更多
【Aperson】
4楼-- · 2019-04-12 15:49

You can simply press PrtScr button (windows will copy whole descktop image to buffer), then paste it in Power Point and crop if you like.

查看更多
登录 后发表回答