Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture the screen shot using .NET [duplicate]

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.

like image 352
Bhavik Goyal Avatar asked Feb 19 '11 05:02

Bhavik Goyal


People also ask

How to take a screenshot in Windows 10?

Similarly, you can capture any part of the System, just minimize the browser window of our application and click on the capture button, it will capture the screenshot such as follows: As you have seen above, I have captured a screenshot of my desktop.

How do I create a capturescreenshots project?

"File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page). Provide the Project name such as CaptureScreenShots or another as you wish and specify the location.

How to take snap shots of the application screen?

We use the width and height of the application screen to capture the snap shot. So the user can place our application screen on any area of the screen, move/resize to get the snap shot. To make the user more comfortable in selecting the snap shot area we make the application screen semi-transparent by setting the opacity of the form to 50%.

Is it possible to take screenshots from a NET Core Library?

Therefore you cannot do it directly from a .net core library. What you can do is implement platform-specific screenshot grabbing in libraries of respective types: win, android, mac, etc. and connect it to the facilities of you cross-platform code over a DI-container, for example.


1 Answers

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.

like image 127
Cody Gray Avatar answered Sep 23 '22 02:09

Cody Gray