捕捉屏幕截图使用.NET截图、屏幕、NET

2023-09-02 01:22:22 作者:素衣白裙清浅微笑

可能重复:   我可以如何捕获屏幕的位图?

Possible Duplicate: How May I Capture the Screen in a Bitmap?

我需要捕捉当前画面,当特定按钮被击中的快照的应用程序。

I need to make an application that captures a snapshot of the current screen whenever a particular button is hit.

我寻觅了很多,但我只找到如何捕捉当前窗口。

I have searched a lot, but I have only found how to capture the current window.

能否请你帮我找出如何做到这一点的。NET?

Can you please help me figure out how to do this in .NET?

我们可以通过点击打印屏幕,并使用烤漆保存图像手动执行此操作。我需要做同样的事情,但我想用一个程序这样做。

We can do this manually by hitting print-screen and saving the image using the paint. I need to do the same thing, but I want to do so with a program.

推荐答案

这当然可以抓住使用.NET Framework的屏幕截图。最简单的方法是创建一个新的位图对象,并绘制成使用Graphics.CopyFromScreen方法。

It's certainly possible to grab a screenshot using the .NET Framework. The simplest way is to create a new Bitmap object and draw into that using the Graphics.CopyFromScreen method.

样品code:

using (Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
                                            Screen.PrimaryScreen.Bounds.Height))
{
    using (Graphics g = Graphics.FromImage(bmpScreenCapture))
    {
        g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
                         Screen.PrimaryScreen.Bounds.Y,
                         0, 0,
                         bmpScreenCapture.Size,
                         CopyPixelOperation.SourceCopy);
    }
}

警告::此方法不能正确地进行分层窗口工作。汉斯帕桑特的回答here解释了需要更复杂的方法来得到那些在你的屏幕截图。

Caveat: This method doesn't work properly for layered windows. Hans Passant's answer here explains the more complicated method required to get those in your screen shots.