Saving a Screenshot Using C#

Introduction

This article explains the entire scenario for creating an image containing a screen content, like windows and other details on the screen. No timer is set in this, each time the application runs it will save the image inside your Documents folder where you can access it.

Background

Today, I saw the article from a fellow CodeProject user, who tried his best to teach us how to save the screen shot from the screen. But it contained a few bugs and it was not very efficient since a major part of the article was missing and he had not shown the code to the users.

Assemblies Required

This project, like all others, requires some assemblies. To include them in your project, you make a reference to the namespaces. You can use the Add Reference dialog from the Visual Studio to do so (I am not sure about other IDEs).

The required namespaces to be called for this project are as follows:

 

  1. using System.Drawing;  
  2. using System.IO;  
  3. using System.Windows.Forms;  

 

System.Drawing

This namespace is required for graphics, bitmap and so on. Without this, they won't work and you will get an error by the IntelliSense saying this is not found.

System.IO

This namespace is required to save the (image) file. As the name states, it is a namespace for the Input/output commands. Saving and deleting files inside the file system is done using this!

The remaining code is just a simple console application to check for the details on the screen and convert them to the Graphics to be saved in a PNG file.

Using the Code

The code for the project is as follows:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Drawing;  
  7. using System.IO;  
  8.   
  9. namespace Test_Application_C_Sharp  
  10. {  
  11.     class Program  
  12.     {  
  13.         // a method to pause the console.  
  14.         // not a part of this project!  
  15.         public static void pause()  
  16.         {  
  17.             Console.Read();  
  18.         }  
  19.   
  20.         static void Main(string[] args)  
  21.         {  
  22.             // Start the process...  
  23.             Console.WriteLine("Starting the process...");  
  24.             Console.WriteLine();  
  25.             Bitmap memoryImage;  
  26.             memoryImage = new Bitmap(1000, 900);  
  27.             Size s = new Size(memoryImage.Width, memoryImage.Height);  
  28.   
  29.             Graphics memoryGraphics = Graphics.FromImage(memoryImage);  
  30.   
  31.             memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);  
  32.   
  33.             //That's it! Save the image in the directory and this will work like charm.  
  34.             string fileName = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +  
  35.                       @"\Screenshot" + "_" +   
  36.                       DateTime.Now.ToString("(dd_MMMM_hh_mm_ss_tt)") + ".png");  
  37.   
  38.             // save it  
  39.             memoryImage.Save(fileName);  
  40.   
  41.             // Write the message,  
  42.             Console.WriteLine("Picture has been saved...");  
  43.             // Pause the program to show the message.  
  44.             Program.pause();  
  45.         }  
  46.     }  
  47. }  

 

You can see that the code is really very interesting in some places.

You create the Graphics from the Image that actually is a Bitmap of 1000x900 size. Once done, you take the pixels from the screen and paste them onto the graphics canvas like:

 

  1. // Create graphics from the Image (Bitmap)  
  2. Graphics memoryGraphics = Graphics.FromImage(memoryImage);  
  3.   
  4. // Copy data from screen  
  5. memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);  

 

Note: s is the Size of the Bitmap itself, see the code.

I am using the size of the Bitmap image to create the graphics and then saving the number of content on the Graphics canvas.

 

  1. Bitmap memoryImage;  
  2. memoryImage = new Bitmap(1000, 900);  
  3. Size s = new Size(memoryImage.Width, memoryImage.Height);  

 

Similarly, the folder and the image name are now dynamic and will always point to your Documents folder, using the special folders on the .NET Framework.

 

  1. Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Screenshot.png"  

 

..Will give you the folder and the last string is the name of the image. That is "screenshot.png".

After all this, you just call the Save method to save the Bitmap.

 

  1. memoryImage.Save(str);  

 

Result on Screen


Points of Interest

I have learned that there is no such thing as a Graphics constructor in the entire .NET Framework.