FormMain.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. using Emgu.CV.Face;
  2. using Emgu.CV.Structure;
  3. using Emgu.CV.Util;
  4. using Emgu.CV;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using System.IO;
  15. using System.Windows.Media.Imaging;
  16. using Emgu.CV.CvEnum;
  17. using System.Runtime.CompilerServices;
  18. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  19. using System.Data.SqlClient;
  20. namespace T_FaceRecognizer
  21. {
  22. public partial class FormMain : Form
  23. {
  24. public FormMain()
  25. {
  26. InitializeComponent();
  27. }
  28. private void CaptureTimer_Tick(object sender, EventArgs e)
  29. {
  30. ProcessFrame();
  31. }
  32. #region <Переменные>
  33. SqlConnection SCon = new SqlConnection(@"Data Source=213.155.192.79,3002;Initial Catalog=FaceTrackApp;Persist Security Info=True;User ID=u20teresh;Password=bfg2");
  34. public int SelectedCameraID { get; set; } = 0;
  35. Rectangle CurrentFace;
  36. public string UserName { get; set; } = "Лицо не обнаружено";
  37. /// <summary>
  38. /// ID пользователя, который обнаружен
  39. /// </summary>
  40. private string CurrentUserID = string.Empty;
  41. /// <summary>
  42. /// тип операции 1 - вход, 0- выход
  43. /// </summary>
  44. private int TypeOperation = 0;
  45. #endregion
  46. #region <Свойства>
  47. public event PropertyChangedEventHandler PropertyChanged;
  48. private VideoCapture Capture;
  49. private CascadeClassifier HaarCascade;
  50. private Image<Bgr, Byte> BgrFrame = null;
  51. private Image<Gray, Byte> DetectedFace = null;
  52. private List<FaceData> FaceList = new List<FaceData>();
  53. private VectorOfMat ImageList = new VectorOfMat();
  54. private List<string> NameList = new List<string>();
  55. private VectorOfInt LabelList = new VectorOfInt();
  56. private EigenFaceRecognizer recognizer;
  57. private Timer CaptureTimer;
  58. private Timer CaptureTimerIdent;
  59. private bool IsRecognized = false;
  60. #region FaceName
  61. private string faceName;
  62. public string FaceName
  63. {
  64. get { return faceName; }
  65. set
  66. {
  67. faceName = value;
  68. if (faceName == "Лицо не обнаружено")
  69. {
  70. IsRecognized = false;
  71. }
  72. else
  73. {
  74. IsRecognized = true;
  75. }
  76. UserName = faceName;
  77. NotifyPropertyChanged();
  78. }
  79. }
  80. #endregion
  81. #region CameraCaptureImage
  82. private Bitmap cameraCapture;
  83. public Bitmap CameraCapture
  84. {
  85. get { return cameraCapture; }
  86. set
  87. {
  88. cameraCapture = value;
  89. if (TabPages.SelectedIndex == 1)
  90. {
  91. DrawName(cameraCapture, CurrentFace);
  92. PbxIdentification.Image = cameraCapture;
  93. }
  94. else
  95. {
  96. PbxFaces.Image = cameraCapture;
  97. }
  98. NotifyPropertyChanged();
  99. }
  100. }
  101. #endregion
  102. #region CameraCaptureFaceImage
  103. private Bitmap cameraCaptureFace;
  104. public Bitmap CameraCaptureFace
  105. {
  106. get { return cameraCaptureFace; }
  107. set
  108. {
  109. cameraCaptureFace = value;
  110. PbxFaces.Image = cameraCapture;
  111. NotifyPropertyChanged();
  112. }
  113. }
  114. #endregion
  115. #endregion
  116. /// <summary>
  117. /// отрисовка имени над прямоугольком
  118. /// </summary>
  119. private void DrawName(Bitmap cameraCapture, Rectangle face)
  120. {
  121. Pen PenForFace = new Pen(Brushes.Red, 5);
  122. Brush BrushForFace = Brushes.White;
  123. Font MyFont = new Font("Segoe UI Variable Small Semibol; 12pt; style=Bold", 12, FontStyle.Regular);
  124. Brush BrushInfo = Brushes.White;
  125. Pen PenInfo = new Pen(Brushes.White, 4);
  126. Graphics Graph = Graphics.FromImage(cameraCapture);
  127. //Graph.DrawRectangle(PenForFace, face.X, face.Y, face.Width, face.Height);
  128. //позиция отрисовки имени человека
  129. Point PointName = new Point(face.X, face.Y - 25);
  130. Graph.DrawString($"{UserName}", MyFont, BrushForFace, PointName);
  131. }
  132. /// <summary>
  133. /// получение данных с камеры и обработка изображени
  134. /// </summary>
  135. private void ProcessFrame()
  136. {
  137. BgrFrame = Capture.QueryFrame().ToImage<Bgr, Byte>().Flip(FlipType.Horizontal);
  138. if (BgrFrame != null)
  139. {
  140. try
  141. {//for emgu cv bug
  142. Image<Gray, byte> grayframe = BgrFrame.Convert<Gray, byte>();
  143. Rectangle[] faces = HaarCascade.DetectMultiScale(grayframe, 1.2, 10, new System.Drawing.Size(50, 50), new System.Drawing.Size(200, 200));
  144. //detect face
  145. FaceName = "No face detected";
  146. foreach (var face in faces)
  147. {
  148. BgrFrame.Draw(face, new Bgr(255, 255, 0), 2);
  149. DetectedFace = BgrFrame.Copy(face).Convert<Gray, byte>();
  150. FaceRecognition();
  151. break;
  152. }
  153. CameraCapture = BgrFrame.ToBitmap();
  154. }
  155. catch (Exception ex)
  156. {
  157. //todo log
  158. }
  159. }
  160. }
  161. /// <summary>
  162. /// распознавание лица
  163. /// </summary>
  164. private void FaceRecognition()
  165. {
  166. if (ImageList.Size != 0)
  167. {
  168. //Eigen Face Algorithm
  169. FaceRecognizer.PredictionResult result = recognizer.Predict(DetectedFace.Resize(100, 100, Inter.Cubic));
  170. FaceName = NameList[result.Label];
  171. CurrentUserID = FaceList[result.Label].UserID;
  172. CameraCaptureFace = DetectedFace.ToBitmap();
  173. }
  174. else
  175. {
  176. FaceName = "Пожалуйста добавьте лицо";
  177. }
  178. }
  179. /// <summary>
  180. /// Convert bitmap to bitmap image for image control
  181. /// </summary>
  182. /// <param name="bitmap">Bitmap image</param>
  183. /// <returns>Image Source</returns>
  184. private Bitmap BitmapToImageSource(Bitmap bitmap)
  185. {
  186. using (MemoryStream memory = new MemoryStream())
  187. {
  188. bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
  189. memory.Position = 0;
  190. BitmapImage bitmapimage = new BitmapImage();
  191. bitmapimage.BeginInit();
  192. bitmapimage.StreamSource = memory;
  193. bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
  194. bitmapimage.EndInit();
  195. //конвертация BitmapImage в Bitmap
  196. BitmapEncoder Enc = new BmpBitmapEncoder();
  197. Enc.Frames.Add(BitmapFrame.Create(bitmapimage));
  198. Enc.Save(memory);
  199. Bitmap Btm = new Bitmap(memory);
  200. return new Bitmap(Btm);
  201. }
  202. }
  203. /// <summary>
  204. /// добавление лица в систему
  205. /// </summary>
  206. private void BtnAdd_Click(object sender, EventArgs e)
  207. {
  208. if (DetectedFace == null)
  209. {
  210. MessageBox.Show("Лица не обнаружены!\r\nУбедитесь что лицо находится в кадре и обведено красным прямоугольником.", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Error);
  211. return;
  212. }
  213. string PhotoPath = string.Empty;
  214. //ProgressOperation.Value = 10;
  215. //TslStatus.Text = "Добавление нового пользователя...";
  216. bool Flag = TbxLastname.Text.Trim() == "" || TbxName.Text.Trim() == "" || TbxMaskPhone.Text.Trim() == "" || TbxMaskSnils.Text.Trim() == "";
  217. if (Flag)
  218. {
  219. MessageBox.Show("Заполните все поля и повторите попытку!", "Ошибка добавления!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  220. return;
  221. }
  222. SCon.Open();
  223. string QueryCheckPassport = $@"select UserID
  224. from Users
  225. where Snils = '{TbxMaskSnils.Text.Trim()}'";
  226. SqlCommand CmdCheck = new SqlCommand(QueryCheckPassport, SCon);
  227. SqlDataReader Res = CmdCheck.ExecuteReader();
  228. if (Res.HasRows)
  229. {
  230. MessageBox.Show("Такой пользователь уже зарегистрирован в системе!", "FaceTrack", MessageBoxButtons.OK, MessageBoxIcon.Error);
  231. SCon.Close();
  232. return;
  233. }
  234. SCon.Close();
  235. //изменить размеры изображения
  236. DetectedFace = DetectedFace.Resize(100, 100, Inter.Cubic);
  237. //сохранить изображение в папку
  238. DetectedFace.Save(Config.FacePhotosPath + $"{TbxName.Text.Trim()}{TbxMaskSnils.Text.Trim()}" + Config.ImageFileExtension);
  239. PhotoPath = Config.FacePhotosPath + $"{TbxName.Text.Trim()}{TbxMaskSnils.Text.Trim()}" + Config.ImageFileExtension;
  240. //добавление пользователя в БД
  241. SCon.Open();
  242. string QueryAdd = $@"insert into Users (Lastname,Firstname,Patronymic,Phone,Snils,Photo)
  243. values (@last,@first,@patr,@phone,@snils,@photo)
  244. ";
  245. SqlCommand Cmd = new SqlCommand(QueryAdd, SCon);
  246. Cmd.Parameters.AddWithValue("@last", TbxLastname.Text.Trim());
  247. Cmd.Parameters.AddWithValue("@first", TbxName.Text.Trim());
  248. Cmd.Parameters.AddWithValue("@patr", TbxPatronymic.Text.Trim());
  249. Cmd.Parameters.AddWithValue("@phone", TbxMaskPhone.Text.Trim());
  250. Cmd.Parameters.AddWithValue("@snils", TbxMaskSnils.Text.Trim());
  251. Cmd.Parameters.AddWithValue("@photo", PhotoPath);
  252. Cmd.ExecuteNonQuery();//выполнить запрос
  253. SCon.Close();
  254. MessageBox.Show("Успешно сохранено!", "FaceTrack", MessageBoxButtons.OK, MessageBoxIcon.Information);
  255. }
  256. /// <summary>
  257. /// получение данных об изображениях
  258. /// </summary>
  259. public void GetFacesList()
  260. {
  261. //haar cascade classifier
  262. if (!File.Exists(Config.HaarCascadePath))
  263. {
  264. string text = "Файл каскада Хаара не обнаружен:\n\n";
  265. text += Config.HaarCascadePath;
  266. DialogResult result = MessageBox.Show(text, "Error",
  267. MessageBoxButtons.OK, MessageBoxIcon.Error);
  268. }
  269. HaarCascade = new CascadeClassifier(Config.HaarCascadePath);
  270. FaceList.Clear();
  271. // Создать директорию, если она отсутствовала
  272. if (!Directory.Exists(Config.FacePhotosPath))
  273. {
  274. Directory.CreateDirectory(Config.FacePhotosPath);
  275. }
  276. // Тренировать изображения
  277. if (ImageList.Size > 0)
  278. {
  279. recognizer = new EigenFaceRecognizer(ImageList.Size);
  280. recognizer.Train(ImageList, LabelList);
  281. }
  282. }
  283. protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
  284. {
  285. var handler = PropertyChanged;
  286. if (handler != null)
  287. handler(this, new PropertyChangedEventArgs(propertyName));
  288. }
  289. private void FormMain_Load(object sender, EventArgs e)
  290. {
  291. //скрытие заголовков вкладок в TabControl
  292. TabPages.Appearance = TabAppearance.FlatButtons;
  293. TabPages.ItemSize = new Size(0, 1);
  294. TabPages.SizeMode = TabSizeMode.Fixed;
  295. TabPages.SelectTab(4);
  296. }
  297. private void PbxMenu_Click(object sender, EventArgs e)
  298. {
  299. if(SplitContainer.Panel1.Width != 45)
  300. {
  301. SplitContainer.SplitterDistance = 45;
  302. PanHome.BackColor = Color.FromArgb(243, 243, 243);
  303. PanAdd.BackColor = Color.FromArgb(243, 243, 243);
  304. PanIdentification.BackColor = Color.FromArgb(243, 243, 243);
  305. PanJournal.BackColor = Color.FromArgb(243, 243, 243);
  306. PanSettings.BackColor = Color.FromArgb(243, 243, 243);
  307. }
  308. else
  309. {
  310. SplitContainer.SplitterDistance = 250;
  311. PanHome.BackColor = Color.FromArgb(58, 166, 64);
  312. PanAdd.BackColor = Color.FromArgb(58, 166, 64);
  313. PanIdentification.BackColor = Color.FromArgb(58, 166, 64);
  314. PanJournal.BackColor = Color.FromArgb(58, 166, 64);
  315. PanSettings.BackColor = Color.FromArgb(58, 166, 64);
  316. }
  317. }
  318. private void FormMain_Resize(object sender, EventArgs e)
  319. {
  320. //PbxMenu_Click(sender, e);
  321. }
  322. private void FormMain_ResizeEnd(object sender, EventArgs e)
  323. {
  324. //PbxMenu_Click(sender, e);
  325. }
  326. private void BtnMain_Click(object sender, EventArgs e)
  327. {
  328. TabPages.SelectTab(4);
  329. }
  330. private void BtnAddUser_Click(object sender, EventArgs e)
  331. {
  332. //RecognizeOff();
  333. CaptureTimer = new Timer()
  334. {
  335. Interval = Config.TimerResponseValue
  336. };
  337. CaptureTimer.Tick += CaptureTimer_Tick;
  338. GetFacesList();
  339. Capture = new VideoCapture(Config.ActiveCameraIndex);
  340. Capture.SetCaptureProperty(CapProp.Fps, 30);
  341. Capture.SetCaptureProperty(CapProp.FrameHeight, 450);
  342. Capture.SetCaptureProperty(CapProp.FrameWidth, 370);
  343. CaptureTimer.Start();
  344. TabPages.SelectTab(0);
  345. }
  346. private void BtnIdent_Click(object sender, EventArgs e)
  347. {
  348. CaptureTimerIdent = new Timer()
  349. {
  350. Interval = Config.TimerResponseValue
  351. };
  352. CaptureTimerIdent.Tick += CaptureTimerIdent_Tick;
  353. try
  354. {
  355. RecognizeOff();
  356. }
  357. catch (Exception ex)
  358. {
  359. MessageBox.Show(ex.Message, "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  360. }
  361. GetFacesListForIdentification();
  362. Capture = new VideoCapture(SelectedCameraID);
  363. //настройка кадров
  364. Capture.SetCaptureProperty(CapProp.Fps, 30);
  365. Capture.SetCaptureProperty(CapProp.FrameHeight, 450);
  366. Capture.SetCaptureProperty(CapProp.FrameWidth, 370);
  367. CaptureTimerIdent.Start();
  368. TabPages.SelectTab(1);
  369. }
  370. /// <summary>
  371. /// отключение камеры и распознавания
  372. /// </summary>
  373. private void RecognizeOff()
  374. {
  375. if (BgrFrame != null)
  376. {
  377. BgrFrame = null;
  378. Capture.Dispose();
  379. if (CaptureTimer != null)
  380. {
  381. CaptureTimer.Tick -= CaptureTimer_Tick;
  382. }
  383. }
  384. }
  385. private void CaptureTimerIdent_Tick(object sender, EventArgs e)
  386. {
  387. ProcessFrameIndentification();
  388. }
  389. /// <summary>
  390. /// получение данных с камеры и распознавание
  391. /// </summary>
  392. private void ProcessFrameIndentification()
  393. {
  394. BgrFrame = Capture.QueryFrame().ToImage<Bgr, Byte>().Flip(FlipType.Horizontal);
  395. if (BgrFrame != null)
  396. {
  397. try
  398. {//for emgu cv bug
  399. Image<Gray, byte> grayframe = BgrFrame.Convert<Gray, byte>();
  400. Rectangle[] faces = HaarCascade.DetectMultiScale(grayframe, 1.2, 10, new System.Drawing.Size(50, 50), new System.Drawing.Size(200, 200));
  401. FaceName = "Лицо не обнаружено";
  402. foreach (var face in faces)
  403. {
  404. CurrentFace = face;
  405. BgrFrame.Draw(face, new Bgr(53, 23, 247), 2);
  406. DetectedFace = BgrFrame.Copy(face).Convert<Gray, byte>();
  407. FaceRecognition();
  408. break;
  409. }
  410. CameraCapture = BgrFrame.ToBitmap();
  411. }
  412. catch (Exception ex)
  413. {
  414. MessageBox.Show(ex.Message);
  415. }
  416. }
  417. }
  418. private void GetFacesListForIdentification()
  419. {
  420. //файл Хаара
  421. if (!File.Exists(Config.HaarCascadePath))
  422. {
  423. string text = "Не удаётся найти файл данных - каскад Хаара:\n\n";
  424. text += Config.HaarCascadePath;
  425. DialogResult result = MessageBox.Show(text, "Ошибка",
  426. MessageBoxButtons.OK, MessageBoxIcon.Error);
  427. }
  428. HaarCascade = new CascadeClassifier(Config.HaarCascadePath);
  429. FaceList.Clear();
  430. FaceData FaceItem = null;
  431. //создать папку для фото, если она отсутствовала
  432. if (!Directory.Exists(Config.FacePhotosPath))
  433. {
  434. Directory.CreateDirectory(Config.FacePhotosPath);
  435. }
  436. //получить из БД инфо о пользователе (id, имя, фамилия, путь до фото)
  437. SCon.Open();
  438. string QueryGetInfoAboutUser = $@"select UserID ,Users.Lastname, Users.Firstname, Users.Patronymic, Photo
  439. from Users";
  440. SqlCommand Cmd = new SqlCommand(QueryGetInfoAboutUser, SCon);
  441. SqlDataReader Res = Cmd.ExecuteReader();
  442. if (Res.HasRows)
  443. {
  444. while (Res.Read())
  445. {
  446. FaceItem = new FaceData();
  447. FaceItem.FaceImage = new Image<Gray, byte>(Application.StartupPath + "\\" + Res["Photo"].ToString());
  448. FaceItem.PersonName = Res["Firstname"].ToString();
  449. FaceItem.LastName = Res["Lastname"].ToString();
  450. FaceItem.Patronymic = Res["Patronymic"].ToString();
  451. FaceItem.UserID = Res["UserID"].ToString();
  452. FaceList.Add(FaceItem);
  453. }
  454. }
  455. else
  456. {
  457. SCon.Close();
  458. MessageBox.Show("Данные о пользователях отсутствуют!", "FaceTrack", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  459. return;
  460. }
  461. SCon.Close();
  462. int i = 0;
  463. foreach (var face in FaceList)
  464. {
  465. ImageList.Push(face.FaceImage.Mat);
  466. NameList.Add(face.PersonName + " " + face.LastName);
  467. LabelList.Push(new[] { i++ });
  468. }
  469. // Тренировка распознавания
  470. if (ImageList.Size > 0)
  471. {
  472. recognizer = new EigenFaceRecognizer(ImageList.Size);
  473. recognizer.Train(ImageList, LabelList);
  474. }
  475. }
  476. private void BtnIn_Click(object sender, EventArgs e)
  477. {
  478. TypeOperation = 1;
  479. RegisterVisit(TypeOperation);
  480. }
  481. private void RegisterVisit(int typeOperation)
  482. {
  483. if (!IsRecognized)
  484. {
  485. MessageBox.Show("Убедитесь, что лицо находится в кадре и обведено красным прямоугольником!", "Ошибка распознавания!");
  486. return;
  487. }
  488. if (typeOperation == 1)
  489. {
  490. #region Предупреждение при попытке повторной идентификации пользователя за один день
  491. SCon.Open();
  492. string QueryCheckExistsVisit = $@"select Count(ID) as Cnt
  493. from Visits
  494. where UsersID = '{CurrentUserID}' and [Date] = CAST(GETDATE() as date)";
  495. SqlCommand CmdCheckVisit = new SqlCommand(QueryCheckExistsVisit, SCon);
  496. SqlDataReader Res = CmdCheckVisit.ExecuteReader();
  497. Res.Read();
  498. int n = int.Parse(Res["Cnt"].ToString());
  499. if (n >= 1)
  500. {
  501. MessageBox.Show("Сегодня данный пользователь уже прошёл идентификацию при входе!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  502. SCon.Close();
  503. return;
  504. }
  505. SCon.Close();
  506. #endregion
  507. //регистрация посещения
  508. SCon.Open();
  509. string QueryAddVisit = $@"set dateformat dmy
  510. insert into Visits (UsersID,[Date],TimeEntrance)
  511. values ('{CurrentUserID}',CAST(GETDATE() as date),'{DateTime.Now.ToString("HH:mm:ss")}')
  512. ";
  513. SqlCommand Cmd = new SqlCommand(QueryAddVisit, SCon);
  514. Cmd.ExecuteNonQuery();
  515. SCon.Close();
  516. MessageBox.Show("Вход прошёл!");
  517. }
  518. else
  519. {
  520. SCon.Open();
  521. string QueryCheckRecord = $@"set dateformat dmy
  522. select *
  523. from Visits
  524. where UsersID = '{CurrentUserID}' and [Date] = cast(GETDATE() as date) and TimeExit is null
  525. ";
  526. SqlCommand CmdCheck = new SqlCommand(QueryCheckRecord, SCon);
  527. SqlDataReader Res = CmdCheck.ExecuteReader();
  528. if (!Res.HasRows)
  529. {
  530. MessageBox.Show("Данный пользователь уже вышел!", "ImpulseVision", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  531. SCon.Close();
  532. return;
  533. }
  534. SCon.Close();
  535. //!!! Выход пользователя, который не прошёл идентификацию не регистрируется
  536. SCon.Open();
  537. string QueryTimeExit = $@"update Visits
  538. set TimeExit = '{DateTime.Now.ToString("HH:mm:ss")}'
  539. where UsersID = '{CurrentUserID}' and [Date] = cast(GETDATE() as date)";
  540. SqlCommand Cmd = new SqlCommand(QueryTimeExit, SCon);
  541. Cmd.ExecuteNonQuery();
  542. SCon.Close();
  543. MessageBox.Show("Выход зарегистрирован!");
  544. }
  545. }
  546. private void BtnOut_Click(object sender, EventArgs e)
  547. {
  548. TypeOperation = 0;
  549. RegisterVisit(TypeOperation);
  550. }
  551. private void BtnJournal_Click(object sender, EventArgs e)
  552. {
  553. RecognizeOff();
  554. GetInformationAboutVisiting();
  555. TabPages.SelectTab(2);
  556. }
  557. /// <summary>
  558. /// получение списка пользователей для журнала посещений
  559. /// </summary>
  560. private void GetInformationAboutVisiting()
  561. {
  562. DgbJournal.Rows.Clear();
  563. SCon.Open();
  564. string QueryGetVisits = $@"select UserID,Lastname +' '+Firstname+' '+Patronymic as FIO,(select MAX([Date])
  565. from Visits
  566. where UsersID = Users.UserID) as LastDateVisit
  567. from Users";
  568. SqlCommand CommandGetVisits = new SqlCommand(QueryGetVisits, SCon);
  569. SqlDataReader Res = CommandGetVisits.ExecuteReader();
  570. if(Res.HasRows)
  571. {
  572. string DateTemplate = string.Empty;
  573. while(Res.Read())
  574. {
  575. DateTemplate = DateTime.Parse(Res["LastDateVisit"].ToString()).ToString("dd.MM.yyyy");
  576. DgbJournal.Rows.Add(Res["UserID"].ToString(), Res["FIO"].ToString(), DateTemplate);
  577. }
  578. }
  579. else
  580. {
  581. MessageBox.Show("Посещения пользователей отсутствуют!", "FaceTrack", MessageBoxButtons.OK, MessageBoxIcon.Error);
  582. SCon.Close();
  583. return;
  584. }
  585. SCon.Close();
  586. }
  587. private void DgbJournal_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  588. {
  589. int RowIndex = e.RowIndex;
  590. int SelectedUserID = int.Parse(DgbJournal.Rows[RowIndex].Cells[0].Value.ToString());
  591. string SelectedUserName = DgbJournal.Rows[RowIndex].Cells[1].Value.ToString();
  592. FormVisiting FVisiting = new FormVisiting(SelectedUserID, SelectedUserName);
  593. this.Hide();
  594. FVisiting.ShowDialog();
  595. this.Show();
  596. }
  597. }
  598. }