9

I am creating an app that takes a screenshot of the desktop at a certain interval of time.

Code:

String nme = "";
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
                                Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
nme = DateTime.Now.ToString();
printscreen.Save(@"F:\Temp\printScre.jpg", ImageFormat.Jpeg);
  1. Is CopyFromScreen a right way to get the screenshot of the desktop?
  2. If I want to send this screenshot to the remote server, then what should be the best way to do that?
  3. Should I do some compression before sending it, and if so what are the most efficient ways?
LW001
  • 1,812
  • 4
  • 19
  • 28
hridya
  • 163
  • 1
  • 2
  • 13

2 Answers2

8
  1. Yes, it is right way
  2. There are a few possibilities. I recommend sending it via email or FTP (as it's simple in c#).
  3. No, personally I don't think you need additional compression. Your screenshots are already saved as JPEG, so they are already compressed.

Code snippet for sending email with attachment:

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(YOUR SMTP SERVER ADDRESS);
mail.From = new MailAddress(SENDER ADDRESS);
mail.To.Add(RECEIVER ADDRESS);
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("YOURFILE");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(YOUR_SMTP_USER_NAME, YOUR_SMTP_PASSWORD);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail); 

(based on this blog)

Code snippet for FTP:

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
    StreamReader sourceStream = new StreamReader("testfile.txt");
    byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();
    request.ContentLength = fileContents.Length;
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContents, 0, fileContents.Length);
    requestStream.Close();
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();    
    Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);    
    response.Close();

(based on MSDN)

  • thx.. but i am developing a winform app.. writing networkCredentials details would not be a safe way , any other way of sending images to remote server, please ? – hridya Oct 14 '13 at 06:57
3

Yes, it is a perfectly right way. Although, I recommed you to visit the following links here on SO, because there are already answers to your question in some form.

Another solution with CopyFromScreen and more complicated solution with some insight on the problem.

As for sending to a remote server, consider using TcpClient or email. Compression is always a good idea if you take more screens or if network is slow, dics space is low, etc. Regarding compression efficiency, look at some archivators available out there and take what you need. They vary mainly in a compression format, speed and compression quality, but you can almost with all of them decide if you need speed or quality.

Community
  • 1
  • 1
Ondrej Janacek
  • 11,896
  • 14
  • 52
  • 87
  • 2
    ,thanks for quick response but are u sure that CopyFromScreen is a right way ? because i read somewhere that sometimes it send blank screen shots.. – hridya Oct 14 '13 at 06:08
  • I don't have that kind of an experience. You have to test it yourself and find out if it works for you. Just let it run for a week and you will see whether there are blank screens. It also depends how crucial is that all your screens are all right. If you can afford data looses, just go with the other solution I have suggested you. – Ondrej Janacek Oct 14 '13 at 06:17
  • hridya: There is a problem with getting blank screenshot, when application runs as Windows Service. When it is "normal" application, everythin is OK. – Jakub Szułakiewicz Oct 14 '13 at 06:20
  • @JakubSzułakiewicz thx for clearing this doubt.. and may be it is dumb question but can u please tell me what do you mean by when application runs as Windows Service ? is it mean long running app ?? – hridya Oct 14 '13 at 06:54
  • 1
    @hridya a windows service is a process running without GUI on the background of your operation system – Ondrej Janacek Oct 14 '13 at 08:07