HistoryDataPage.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using CCDCount.DLL;
  2. using CCDCount.DLL.SqlDataClass;
  3. using CCDCount.MODEL.ConfigModel;
  4. using CCDCount.MODEL.ShuLiModel;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Data;
  16. using System.Windows.Documents;
  17. using System.Windows.Input;
  18. using System.Windows.Markup;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace CCDCountWpf.WpfPage
  24. {
  25. /// <summary>
  26. /// HistoryDataPage.xaml 的交互逻辑
  27. /// </summary>
  28. public partial class HistoryDataPage : Page
  29. {
  30. private static ActionMesSqliteDataClass actionMesSqliteDataClass = null;
  31. int DataStartLine = 0;
  32. private int PageHeight = 2000;
  33. public HistoryDataPage()
  34. {
  35. InitializeComponent();
  36. DataContext = ShowMessageBus.ShowBinding;
  37. actionMesSqliteDataClass = new ActionMesSqliteDataClass($"{AppDomain.CurrentDomain.BaseDirectory}DATA\\ActiveObjectData\\Cam{MessageBus.MainThreadS[0].cameraConfig.CamerNo}\\ActiveObjectData{DateTime.Now:yyyyMMdd}.db");
  38. actionMesSqliteDataClass.GetAllActionMinStartMaxEndLine(out int num, out int StartLine, out int EndLine);
  39. ShowMessageBus.ShowBinding.HistoryImageCount = (EndLine - StartLine)/PageHeight;
  40. DataStartLine = StartLine;
  41. UpDateShowBinding();
  42. }
  43. private void PreviousBtn_Click(object sender, RoutedEventArgs e)
  44. {
  45. if (ShowMessageBus.ShowBinding.HistoryImageNum <= 1)
  46. {
  47. MessageBox.Show("已经是第一页");
  48. return;
  49. }
  50. ShowMessageBus.ShowBinding.HistoryImageNum -= 1;
  51. UpDateShowBinding();
  52. }
  53. private void NextBtn_Click(object sender, RoutedEventArgs e)
  54. {
  55. if (ShowMessageBus.ShowBinding.HistoryImageNum >= ShowMessageBus.ShowBinding.HistoryImageCount)
  56. {
  57. MessageBox.Show("已经是最后一页");
  58. return;
  59. }
  60. ShowMessageBus.ShowBinding.HistoryImageNum += 1;
  61. UpDateShowBinding();
  62. }
  63. private void UpDateShowBinding()
  64. {
  65. Stopwatch stopwatch = Stopwatch.StartNew();
  66. BitmapImage ShowBitMap = null;
  67. Bitmap BitmapImage = null;
  68. List<ActiveObjectClass> ShowResult = actionMesSqliteDataClass.GetActiveObjectForPage((ShowMessageBus.ShowBinding.HistoryImageNum - 1) * PageHeight + DataStartLine, ShowMessageBus.ShowBinding.HistoryImageNum * PageHeight + DataStartLine);
  69. if (ShowResult.Count > 0)
  70. {
  71. int ThisImageStartLine = (int)ShowResult.Min(o => o.StartLine);
  72. int ImageHeight = (int)(ShowResult.Max(o => o.LastSeenLine) - ShowResult.Min(o => o.StartLine));
  73. BitmapImage = new Bitmap(4096, ImageHeight <= PageHeight ? PageHeight : ImageHeight);
  74. using (Graphics g = Graphics.FromImage(BitmapImage))
  75. {
  76. g.Clear(System.Drawing.Color.White);
  77. var redPen = new System.Drawing.Pen(System.Drawing.Color.Red, 1);
  78. var bluePen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(0, 146, 255), 1);
  79. var GreenPen = new System.Drawing.Pen(System.Drawing.Color.SeaGreen, 5);
  80. foreach (var item in ShowResult)
  81. {
  82. int roix = item.MinStartCol - 5;
  83. int roiy = (int)(item.StartLine - ThisImageStartLine) - 5;
  84. int roiheight = (int)(item.LastSeenLine - (long)item.StartLine) + 10;
  85. int roiwidth = item.MaxEndCol - item.MinStartCol + 10;
  86. g.DrawRectangle(GreenPen, new System.Drawing.Rectangle(roix, roiy, roiwidth, roiheight));
  87. Font font = new Font("Arial", 40);
  88. g.DrawString(item.Num.ToString(), font, System.Drawing.Brushes.Black, new System.Drawing.Point(roix - 50 * item.Num.ToString().Length, roiy - 20));
  89. foreach (var item1 in item.RowsData)
  90. {
  91. int yPos = (int)(item1.RowsCol - ThisImageStartLine);
  92. g.DrawLine(item.StateCode == 0 ? bluePen : redPen, new System.Drawing.Point(item1.StartCol, yPos), new System.Drawing.Point(item1.EndCol, yPos));
  93. }
  94. }
  95. }
  96. }
  97. else
  98. {
  99. BitmapImage = new Bitmap(4096, PageHeight);
  100. using (Graphics g = Graphics.FromImage(BitmapImage))
  101. {
  102. g.Clear(System.Drawing.Color.White);
  103. }
  104. }
  105. Application.Current.Dispatcher.InvokeAsync(() =>
  106. {
  107. ShowBitMap = ConvertToBitmapImage(BitmapImage);
  108. ShowMessageBus.ShowBinding.HistoryImage = ShowBitMap;
  109. });
  110. stopwatch.Stop();
  111. Console.WriteLine($"{stopwatch.ElapsedMilliseconds}ms");
  112. }
  113. // Bitmap 转 BitmapImage 的辅助方法
  114. private BitmapImage ConvertToBitmapImage(Bitmap bitmap)
  115. {
  116. using (MemoryStream memory = new MemoryStream())
  117. {
  118. bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
  119. memory.Position = 0;
  120. var bitmapImage = new BitmapImage();
  121. bitmapImage.BeginInit();
  122. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  123. bitmapImage.StreamSource = memory;
  124. bitmapImage.EndInit();
  125. return bitmapImage;
  126. }
  127. }
  128. private void SkipBtn_Click(object sender, RoutedEventArgs e)
  129. {
  130. UpDateShowBinding();
  131. }
  132. }
  133. }