Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Saving a Screenshot Using C#, A.K.A Console Monitor

0.00/5 (No votes)
9 Sep 2014 1  
This is an alternative for Console Monitor

Introduction

This tip explains the entire scenario in creating the image that contains the screen content, like the windows and other details on the screen. No timer is set in this, each time the application will run, 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).

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

The code for the project is as follows:

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

Result on Screen

Screenshot image

Points of Interest

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

History

  • First post!
  • Second post: Removed the overusage of Console.WriteLine() and added the code to make sure a new screenshot was to be saved each time the program ran

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here