在表单上捕获表单和元素的屏幕截图

时间:2014-04-28 17:29:27

标签: c# bitmap screenshot

我试图在主窗口上捕获MainWindow的屏幕截图和内容。 MainWindow有时会落后于其他元素,需要在没有其他重叠元素的情况下获取屏幕截图。此代码当前返回空白MainWindow表单的位图,没有任何内容。表单有一堆不同的动态UI元素。如何获取MainWindow及其内容的当前屏幕截图/捕获? MainWindow内容的代码可以很长时间发布,所以我希望这已经足够了。

Rectangle bounds = new Rectangle();
bounds.Width = Program.MainWindow.Width;
bounds.Height = Program.MainWindow.Height;

screenShot = new Bitmap(Program.MainWindow.Width,
                       Program.MainWindow.Height,
                       PixelFormat.Format32bppRgb);
Program.MainWindow.DrawToBitmap(screenShot, bounds);

1 个答案:

答案 0 :(得分:1)

您应该可以使用CopyFromScreen

Graphics方法获取它
        Rectangle bounds = Program.MainWindow.Bounds;
        Bitmap b = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
        using (Graphics g = Graphics.FromImage(b))
        {
            g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
        }
        b.Save("YOUR FILE NAME HERE");