Windows Service for taking screenshots


This is a windows service which takes screenshots at a specific interval. This might be useful to monitor the activities of the user. This will be helful toapplication servers. This is created using C# .NET

Hi,
I have created a windows service which takes screenshots at sa defined interval which can be configured as per the requirement. These screenshots will be saved in a configurable location which can be the same system or any LAN folder.

1.Create a windows service project by selecting New->Project->Windows Service.
Name it ScreenShots.

2.In the service file ScreenShots.cs, place the following code :


public partial class ScreenShots : ServiceBase
{
Timers.Timer timer1 = new Timers.Timer();
public ScreenShots()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
try
{
timer1.Enabled = true;
timer1.Elapsed += new Timers.ElapsedEventHandle(timer1_Elapsed);
timer1.Interval = 2000;
timer1.Start();
}
catch (Exception exp)
{
File.AppendAllText@"D:\LoggerForScrnShotService.txt",exp.Message);
}
}

protected override void OnStop()
{
try
{
File.AppendAllText(@"D:\LoggerForScrnShotService.txt", "Service Stopped");
timer1.Enabled = false;
}
catch (Exception exp)
{
File.AppendAllText(@"D:\LoggerForScrnShotService.txt", exp.Message);
}
}

private void timer1_Elapsed(object sender, EventArgs e)
{
try
{
//Initialise a Bitmap object with the working area of the desktop as the image
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);

//Creates a graphics object from the bitmap image.
Graphics gImage = Graphics.FromImage(bmp);

Point upperLeftSource = new Point(Screen.PrimaryScreen.WorkingArea.X, Screen.PrimaryScreen.WorkingArea.Y);

Point upperLeftDestination = new Point(0, 0);

//The source i.e. the desktop working area will be copied
gImage.CopyFromScreen(upperLeftSource, upperLeftDestination, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);

//Save the file with the current date and time file name as format as jpeg
string fileName = DateTime.Now.ToString("ddMMMyyyy_hhmmss") + ".jpeg";
bmp.Save(@"D:\SaveScreenShots\" + fileName, ImageFormat.Jpeg);

File.AppendAllText(@"D:\LoggerForScrnShotService.txt", "Screenshottaken");
}
catch (Exception exp)
{
File.AppendAllText(@"D:\LoggerForScrnShotService.txt", exp.InnerException.Message);
File.AppendAllText(@"D:\LoggerForScrnShotService.txt", exp.StackTrace);
}
}
}


I have attached the service solution.

After installing the service : the property 'Allow service to interact with the desktop' should be set to true.

Steps : Go to Run -> Type Services.msc -> Find your service in the list of all the installed services -> Right click -> Go to LogOn tab-> Check the 'Allow service to interact with the desktop' property.

Start the service and here you go. The service will be taking the screenshots at any specified interval.

Regards,
Raman S V


Attachments

  • Windows Service for taking screenshots (43993-41522-Windows-Service-taking-screenshots.zip)
  • Comments

    Guest Author: Nishant18 Dec 2012

    It seems you have not tested it on Windows 7 or higher OS.

    It doesn't work in any case as you can't use System.Drawing/System.Drawing.Imaging inside Windows service. It gives black screen shot always.

    Run this program again if you have tested it.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: