41

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.

Community
  • 1
  • 1
Bhavik Goyal
  • 2,756
  • 6
  • 21
  • 41
  • @searock - its not duplicate i have seen that ... but it was as semi transparent window and i need to screen shot the whole screen including the taskbar... Please check it again – Bhavik Goyal Feb 19 '11 at 05:54
  • @Bhavik Goyal You are misunderstanding the title, it means taking a screen shot of a desktop and even display a window if it is semi transparent. No Offense : ) – Searock Feb 19 '11 at 06:06
  • but it means that if my window is at the top and it is surrounding the wholedesktop. But i need to get even if my window is minimized or even my window has been hidden. – Bhavik Goyal Feb 19 '11 at 06:09
  • @Bhavik Goyal In that case you can add the code in a event or a function. – Searock Feb 19 '11 at 06:13
  • @searock I am trying to get this since last 2 days, but i didn't get any success and then after i have asked this question. If you are sure about which event i have to add then please tell me... – Bhavik Goyal Feb 19 '11 at 06:15
  • @Bhavik Goyal Do you mean to say when a user presses a `keyboard button`, your application should take a screen shot? – Searock Feb 19 '11 at 06:19
  • no... i will be taking this using timer after every 5 minutes, my application will be running in hidden mode and after every 5 minutes it will capture the screen shot. so if simply i get the code on the on button press event then its enough... – Bhavik Goyal Feb 19 '11 at 06:21
  • 2
    @Bhavik: I have no idea what you're trying to accomplish. Your attempts to clarify your requirements through comments have only confused me more. If you're trying to take a screenshot every 5 minutes of *whatever* is visible on the user's screen, then create a `Timer` and handle its `Tick` event. In that event, place the code from my answer. Bam, screenshot. – Cody Gray Feb 19 '11 at 06:22
  • About timer i will do ... but main part is how to take a snap shot... i got may document for particular window snapshot but i don't know how to take a snap for whole screen. – Bhavik Goyal Feb 19 '11 at 06:24
  • @Bhavik Goyal It's not good to confuse people who are trying to help you. In your question you have said `I need to make the application that captures the currently screen snapshot whenever a particular button is hitted.` and now you are saying you want in in a timer event. – Searock Feb 19 '11 at 06:27
  • @searock that was answer for your question "Do you mean to say when a user presses a keyboard button, your application should take a screen shot?" – Bhavik Goyal Feb 19 '11 at 06:29
  • 1
    @Bhavik: You say *"i don't know how to take a snap for whole screen"*. Have you tried the code I posted? Does it not work for you? I wrote it essentially from memory; it's possible that I've missed something. Also check out the duplicate question that Gabe linked to; the code shown there looks very similar to my own. One of these samples will *surely* grab an image of the entire screen. Combining it with a `Timer` will allow you to do so every 5 minutes. I don't understand what are you missing? You don't have to justify *why* you're asking the question; you've already asked it. – Cody Gray Feb 19 '11 at 06:29
  • I got the answer to capture the image and i got that from the other source... thanks Cody Gray, Searock and all other who tried to help me... – Bhavik Goyal Feb 19 '11 at 06:49

1 Answers1

56

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.

Tony Edgecombe
  • 3,681
  • 3
  • 25
  • 31
Cody Gray
  • 222,280
  • 47
  • 466
  • 543
  • 4
    It works properly for layered windows in Windows 7+, I think. (Which broke a lot of hacks. Sigh.) – Ry- Jul 12 '13 at 14:57
  • 1
    if change display setting **Change the size of all items** to **larger**, the code can only capture part of the screen. how to solve? – Lei Yang Mar 30 '17 at 07:50