Capture Screen Shots Using ASP.Net C# Without JavaScript

 Admin   
581 View    Dec 12 2019 2:58PM

Background

I have seen many articles in the past few days using a lot of scripting code to capture the screen shots but the .Net Framework capable of doing this type of thin in a few lines of code. So by considering that requirement I decided to write this article especially focusing on beginners and those who want to learn how to capture the screen using ASP.Net and C# without using any scripting language.
 
Let us see step-by-step how to do this.
 
Step 1
 
Create the project as:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".

  2. "File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).

  3. Provide the Project name such as CaptureScreenShots or another as you wish and specify the location.

  4. Then right-click on Solution Explorer and select "Add New Item" then select Default.aspx page.

  5. One Button .
Now the Default.aspx source code will be as follows:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CaptureScreenShots.Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html>  
  5. <head id="Head1" runat="server">  
  6.     <title></title>  
  7. </head>  
  8. <body bgcolor="Silver">  
  9.     <form id="form1" runat="server">  
  10.     <br />  
  11.     <h2 style="color: #808000; font-size: x-large; font-weight: bolder;">  
  12.         Article by Vithal Wadje</h2><br />  
  13.         <asp:Label style="color: #808000;" ID="Label1" runat="server" Text="Label"></asp:Label>  
  14.     <br />  
  15.     <div>  
  16.           
  17.         <br />  
  18.                    <asp:Button   
  19.             ID="Button1" runat="server"  
  20.             Text="Capture" onclick="Button1_Click"  />  
  21.     </div>  
  22.     </form>  
  23. </body>  
  24. </html> 
 Step 2
 
Add a Reference for a Windows Form to use Screen Class Methods.
 
We are using the Screen class of Windows Forms to get our requirement that contains the many methods related to screen shots, so we need to add the reference for Windows Forms in our project. To add a reference, right-click on the project as in the following:
 
 
 
After clicking on Add Reference, one window is shown, click the .Net tab and locate the System.Windows.Form namespace and click on OK. We also need to add a namespace to the top of the class file as in the following:
  1. using System.Windows.Forms; 
 Also we need to use the Graphics class to capture screen shots. So add the following namespaces at the top of the class file as in the following:
  1. using System.Drawing;  
  2. using System.Drawing.Imaging; 
Step 3
 
Create a method to capture the screen shots as in the following:
  1. public static void Capture(string CapturedFilePath)  
  2.       {  
  3.          Bitmap bitmap = new Bitmap  
  4.        (Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);  
  5.   
  6.           Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);  
  7.           graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);  
  8.           
  9.           bitmap.Save(CapturedFilePath, ImageFormat.Bmp);  
  10.       } 
The preceding capture Method takes one parameter, CapturedFilePath, that specifies where to save the captured files. I have saved the files in my E drive, you can also save them on a server folder of the application as you wish.
 
Now call the receding function on button click as in the following:
  1. protected void Button1_Click(object sender, EventArgs e)  
  2.        {  
  3.            Capture( "E:/ScreenShot.bmp");//path to Save Captured files  
  4.        } 
Now the entire code of the default.aspx.cs file will look as in the following:
  1. using System;  
  2. using System.Drawing;  
  3. using System.Drawing.Imaging;  
  4. using System.Windows.Forms;  
  5.   
  6. namespace CaptureScreenShots  
  7. {  
  8.     public partial class Default : System.Web.UI.Page  
  9.     {  
  10.         protected void Page_Load(object sender, EventArgs e)  
  11.         {  
  12.             Label1.Text = "This Screen Shot Taken at "+DateTime.Now.ToString();  
  13.         }  
  14.   
  15.         protected void Button1_Click(object sender, EventArgs e)  
  16.         {  
  17.             Capture( "E:/ScreenShot.bmp");//path to Save Captured files  
  18.         }  
  19.         public static void Capture(string CapturedFilePath)  
  20.         {  
  21.            Bitmap bitmap = new Bitmap  
  22.          (Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);  
  23.   
  24.             Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);  
  25.             graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);  
  26.   
  27.             bitmap.Save(CapturedFilePath, ImageFormat.Bmp);  
  28.         }  
  29.     }  

Now run the application and the page will look as in the following:
 
 
Now click on the capture button, then the screen shot will be saved into the E drive as my given path, the screen shot will look such as follows:
 
 
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 screen shot such as follows:

 
 
 
As you have seen above, I have captured the screen shot of my desktop. Similarly you can capture any part of the system.

Notes
  • Download the Zip file from the attachment for the full source code of the application.

  • Don't forget to specify the location for the captured file.

  • Don't forget to add the reference of Windows.Form.
Summary

I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.