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.
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.
"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.
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%.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With