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