C# - Capture screenshot of active window
This guide presents the following to quickly get you up and running with the Screenshot Capturing Application using C#.
You can download full source code of a Screenshot Capturing Application
Overview
A software or Application capable of Capturing screens will
allow you to take screenshots of your screen iteratively
after a certain period of time (i.e. Here 10 seconds).
after a certain period of time (i.e. Here 10 seconds).
The base thing about this software is, all of these events occured from background. So you can keep yourself busy in other works, while the
application will still capture the screenshots..
It is also possible, that the application or software will upload all of those screenshots to the server..
I'm not going to introduce the above today....We will learn it another day surely....
Key Concepts
You can create a Screenshot Capture software by following the below mentioned
steps:
- First of all put 2 button namely: "Start Capturing" and "Stop Capturing" on the Form.
- Now add a Timer control on the Form.
- Now Click on the Timer Control twice to generate an timer1_Tick event on the code behind.
Put the following code on your code behind: private void timer1_Tick(object sender, EventArgs e) { Image screenShot = CaptureScreenShot(); string filename = "screenshot" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.ToString() + ".jpg"; screenShot.Save("d:/Screens/" + filename); } private Image CaptureScreenShot() { Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb); Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot); gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); return bmpScreenshot; }
Put the following code on your code behind: private void btnCapture_Click(object sender, EventArgs e) { btnCapture.Enabled = false; btnStopCapture.Enabled = true; timer1.Tick += new EventHandler(timer1_Tick); // Everytime timer ticks, timer_Tick will be called timer1.Interval = (1000) * (10); // Timer will tick evert 10 seconds timer1.Enabled = true; // Enable the timer timer1.Start(); // Start the timer } private void btnStopCapture_Click(object sender, EventArgs e) { timer1.Enabled = false; btnCapture.Enabled = true; btnStopCapture.Enabled = false; }
Now run the application and Start getting the screenshots in the directory "D:\Screens"
By, Akash Roy, CEO, JPR Infoserve, http://jprinfoserve.com
No comments:
Post a Comment