Saving a Screenshot Using C#

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).

Required namespaces to be called for this project are as follows:

using System.Drawing;
using System.IO;
using System.Windows.Forms;

System.Drawing

This namespace is required for graphics, bitmap, etc. 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, 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 save inside a PNG file.

Using the Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
 
namespace Test_Application_C_Sharp
{
    class Program
    {
        // a method to pause the console.
        // not a part of this project!
        public static  void pause()
        {
            Console.Read();
        }
 
        static void  Main(string[] args)
        {
            // Start the process...
            Console.WriteLine("Starting the process...");
            Console.WriteLine();
            Bitmap memoryImage;
            memoryImage = new  Bitmap(1000, 900);
            Size s = new  Size(memoryImage.Width, memoryImage.Height);
 
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
 
            memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
 
            //That's it! Save the image in the directory and this will work like charm.
            string fileName = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
                      @"\Screenshot" + "_"  + 
                      DateTime.Now.ToString("(dd_MMMM_hh_mm_ss_tt)") + ".png");
 
            // save it
            memoryImage.Save(fileName);
 
            // Write the message,
            Console.WriteLine("Picture has been saved...");
            // Pause the program to show the message.
            Program.pause();
        }
    }
}

You can see that the code is really very interesting at 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:

// Create graphics from the Image (Bitmap)
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
 
// Copy data from screen
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.

Bitmap memoryImage;
memoryImage = new  Bitmap(1000, 900);
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.

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.

memoryImage.Save(str);

http://www.codeproject.com/KB/cs/817001/Screenshot.png

Points of Interest

The source code above uses Bitmaps and performs a function to capture the graphics from screen. The graphics are then copied to Bitmap and are stored on machine in a form of a screenshot.

Screen capture library

The following library works for Windows Forms, WPF and Console projects.

To capture and window or control the following will return an image base on the window or control handle. In the following code sample pressing the CaptureThisButton button captures the button.

private void  CaptureThisButton_Click(object sender, EventArgs e)
{
    var ops = new  ScreenCapture();
    pictureBox1.Image = ops.CaptureWindow(CaptureThisButton.Handle);
}

To capture the current desktop to a image.

private void  CaptureDesktopButton_Click(object sender, EventArgs e)
{
    var ops = new  ScreenCapture();
    pictureBox2.Image = ops.CaptureScreen();
}

Both of the above methods provide overloaded methods to save a capture to a file. To capture a window or control to an image CaptureWindowToFile and for the desktop CaptureScreenToFile.

Full Visual Studio code.