Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
3.73/5 (7 votes)
9 Sep 2014CPOL2 min read 26.9K   326   14   9
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:

C#
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:

C#
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:

C#
// 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.

C#
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.

C#
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
Afzaal Ahmad Zeeshan is a computer programmer from Rabwah, Pakistan, currently living in The Netherlands, likes .NET Core and Node.js for regular everyday development. Afzaal Ahmad works at Adyen as a Developer Advocate.

He is an expert with Cloud, Mobile, and API development. Afzaal has experience with the Azure platform and likes to build cross-platform libraries/software with .NET Core. Afzaal is an Alibaba Cloud MVP, twice he has been awarded Microsoft MVP status for his community leadership in software development, four times CodeProject MVP status for technical writing and mentoring, and 4 times C# Corner MVP status in the same field.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Sinisa Hajnal12-Sep-14 0:02
professionalSinisa Hajnal12-Sep-14 0:02 
AnswerRe: My vote of 1 Pin
Afzaal Ahmad Zeeshan12-Sep-14 5:01
professionalAfzaal Ahmad Zeeshan12-Sep-14 5:01 
GeneralMy vote of 1 PinPopular
Nicke Olofsson10-Sep-14 6:47
Nicke Olofsson10-Sep-14 6:47 
AnswerRe: My vote of 1 Pin
Afzaal Ahmad Zeeshan10-Sep-14 8:17
professionalAfzaal Ahmad Zeeshan10-Sep-14 8:17 
GeneralRe: My vote of 1 Pin
OriginalGriff12-Sep-14 2:38
mveOriginalGriff12-Sep-14 2:38 
GeneralRe: My vote of 1 Pin
Afzaal Ahmad Zeeshan12-Sep-14 2:42
professionalAfzaal Ahmad Zeeshan12-Sep-14 2:42 
GeneralRe: My vote of 1 Pin
OriginalGriff12-Sep-14 3:19
mveOriginalGriff12-Sep-14 3:19 
GeneralRe: My vote of 1 Pin
Afzaal Ahmad Zeeshan12-Sep-14 5:02
professionalAfzaal Ahmad Zeeshan12-Sep-14 5:02 
AnswerRe: My vote of 1 Pin
Afzaal Ahmad Zeeshan12-Sep-14 5:03
professionalAfzaal Ahmad Zeeshan12-Sep-14 5:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.