.NET 4.x Написание службы для создания скриншотов - C#

Узнай цену своей работы

Формулировка задачи:

Всем привет! Есть код который делает снимок экрана. Пытаюсь переделать его в службу. Просьба помочь.
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.ServiceProcess;
  8. using System.Text;
  9. using System.Threading;
  10. using System.IO;
  11. using System.Drawing.Imaging;
  12. namespace my_service
  13. {
  14.  
  15. public partial class Service1 : ServiceBase
  16. {
  17. private StreamWriter file;
  18. private System.Timers.Timer timer1;
  19. public Service1()
  20. {
  21. InitializeComponent();
  22. }
  23. static /*public*/ string GenerateRandomJpegName()
  24. {
  25. return GenerateRandomString() + ".jpg";
  26. }
  27. static string GenerateRandomString()
  28. {
  29. bool UseSigns = true;
  30. bool UseUpperLetters = true;
  31. bool UseLetters = true;
  32. int Length;
  33. NewLabel:
  34. try
  35. {
  36. Length = new Random(DateTime.Now.Millisecond - DateTime.Now.Second + new Random(DateTime.Now.Millisecond).Next(0, 100) / new Random(DateTime.Now.Millisecond - DateTime.Now.Second).Next(0, 10)).Next(0, 100);
  37. }
  38. catch { goto NewLabel; }
  39. string result = "C:/";
  40. try
  41. {
  42. int Seed = 0;
  43. char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
  44. char[] signs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  45. List<char> keyWords = new List<char>();
  46. List<char> upperLetters = new List<char>();
  47. foreach (char c in letters)
  48. upperLetters.Add(Convert.ToChar(c.ToString().ToUpper()));
  49. if (UseLetters)
  50. foreach (char c in letters)
  51. keyWords.Add(c);
  52. if (UseSigns)
  53. foreach (char c in signs)
  54. keyWords.Add(c);
  55. if (UseUpperLetters)
  56. foreach (char c in upperLetters)
  57. keyWords.Add(c);
  58. int MaxValue = keyWords.Count;
  59. for (int i = 0; i <= Length; i++)
  60. {
  61. try
  62. {
  63. Random mainrand = new Random(Seed);
  64. char RandChar = keyWords[mainrand.Next(0, MaxValue)];
  65. result += RandChar;
  66. Seed += DateTime.Now.Millisecond + Seed - new Random().Next(10) + new Random(DateTime.Now.Millisecond + 800 * 989 / 3).Next(10);
  67. }
  68. catch { continue; }
  69. }
  70. }
  71. catch { }
  72. return result;
  73. }
  74.  
  75. protected override void OnStart(string[] args)
  76. {
  77. Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
  78. Graphics g = Graphics.FromImage(bmp);
  79. g.CopyFromScreen(0, 0, Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
  80. bmp.Save(GenerateRandomJpegName(), ImageFormat.Jpeg);
  81. file = new StreamWriter(new FileStream("my_first_service.txt",
  82. System.IO.FileMode.Append));
  83. this.file.WriteLine("MyFirstService стартовал");
  84. this.file.Flush();
  85. //Создаем таймер и выставляем его параметры
  86. this.timer1 = new System.Timers.Timer();
  87. this.timer1.Enabled = true;
  88. //Интервал 10000мс - 10с.
  89. this.timer1.Interval = 100000;
  90. this.timer1.Elapsed +=
  91. new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
  92. this.timer1.AutoReset = true;
  93. this.timer1.Start();
  94. }
  95. protected override void OnStop()
  96. {
  97. this.timer1.Stop();
  98. this.file.WriteLine("MyFirstService остановлен");
  99. this.file.Flush();
  100. this.file.Close();
  101. }
  102. private void
  103. timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  104. {
  105. //Записываем время в файл или делаем все, что хотим
  106. this.file.WriteLine(DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
  107. this.file.Flush();
  108. }
  109. }
  110. }

Решение задачи: «.NET 4.x Написание службы для создания скриншотов»

textual
Листинг программы
  1. System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
  2. System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

6   голосов , оценка 4.5 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы