.NET 4.x Написание службы для создания скриншотов - C#
Формулировка задачи:
Всем привет! Есть код который делает снимок экрана. Пытаюсь переделать его в службу. Просьба помочь.
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Linq;
- using System.ServiceProcess;
- using System.Text;
- using System.Threading;
- using System.IO;
- using System.Drawing.Imaging;
- namespace my_service
- {
- public partial class Service1 : ServiceBase
- {
- private StreamWriter file;
- private System.Timers.Timer timer1;
- public Service1()
- {
- InitializeComponent();
- }
- static /*public*/ string GenerateRandomJpegName()
- {
- return GenerateRandomString() + ".jpg";
- }
- static string GenerateRandomString()
- {
- bool UseSigns = true;
- bool UseUpperLetters = true;
- bool UseLetters = true;
- int Length;
- NewLabel:
- try
- {
- 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);
- }
- catch { goto NewLabel; }
- string result = "C:/";
- try
- {
- int Seed = 0;
- 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' };
- char[] signs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- List<char> keyWords = new List<char>();
- List<char> upperLetters = new List<char>();
- foreach (char c in letters)
- upperLetters.Add(Convert.ToChar(c.ToString().ToUpper()));
- if (UseLetters)
- foreach (char c in letters)
- keyWords.Add(c);
- if (UseSigns)
- foreach (char c in signs)
- keyWords.Add(c);
- if (UseUpperLetters)
- foreach (char c in upperLetters)
- keyWords.Add(c);
- int MaxValue = keyWords.Count;
- for (int i = 0; i <= Length; i++)
- {
- try
- {
- Random mainrand = new Random(Seed);
- char RandChar = keyWords[mainrand.Next(0, MaxValue)];
- result += RandChar;
- Seed += DateTime.Now.Millisecond + Seed - new Random().Next(10) + new Random(DateTime.Now.Millisecond + 800 * 989 / 3).Next(10);
- }
- catch { continue; }
- }
- }
- catch { }
- return result;
- }
- protected override void OnStart(string[] args)
- {
- Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
- Graphics g = Graphics.FromImage(bmp);
- g.CopyFromScreen(0, 0, Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
- bmp.Save(GenerateRandomJpegName(), ImageFormat.Jpeg);
- file = new StreamWriter(new FileStream("my_first_service.txt",
- System.IO.FileMode.Append));
- this.file.WriteLine("MyFirstService стартовал");
- this.file.Flush();
- //Создаем таймер и выставляем его параметры
- this.timer1 = new System.Timers.Timer();
- this.timer1.Enabled = true;
- //Интервал 10000мс - 10с.
- this.timer1.Interval = 100000;
- this.timer1.Elapsed +=
- new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
- this.timer1.AutoReset = true;
- this.timer1.Start();
- }
- protected override void OnStop()
- {
- this.timer1.Stop();
- this.file.WriteLine("MyFirstService остановлен");
- this.file.Flush();
- this.file.Close();
- }
- private void
- timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- //Записываем время в файл или делаем все, что хотим
- this.file.WriteLine(DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"));
- this.file.Flush();
- }
- }
- }
Решение задачи: «.NET 4.x Написание службы для создания скриншотов»
textual
Листинг программы
- System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
- System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д