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
{
public static void pause()
{
Console.Read();
}
static void Main(string[] args)
{
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);
string fileName = string.Format(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
@"\Screenshot" + "_" +
DateTime.Now.ToString("(dd_MMMM_hh_mm_ss_tt)") + ".png");
memoryImage.Save(fileName);
Console.WriteLine("Picture has been saved...");
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:
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
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
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