HistoryDataPage.xaml.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 = 1000;
  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_{MessageBus.MainThreadS[0].BatchNumber}.db");
  38. //actionMesSqliteDataClass = new ActionMesSqliteDataClass($"{AppDomain.CurrentDomain.BaseDirectory}DATA\\ActiveObjectData\\Cam{MessageBus.MainThreadS[0].cameraConfig.CamerNo}\\ActiveObjectData20250827.db");
  39. actionMesSqliteDataClass.GetAllActionMinStartMaxEndLine(out int num, out int StartLine, out int EndLine);
  40. ShowMessageBus.ShowBinding.HistoryImageCount = (EndLine - StartLine)/PageHeight;
  41. DataStartLine = StartLine;
  42. UpDateShowBinding();
  43. }
  44. private void PreviousBtn_Click(object sender, RoutedEventArgs e)
  45. {
  46. if (ShowMessageBus.ShowBinding.HistoryImageNum <= 1)
  47. {
  48. MessageBox.Show("已经是第一页");
  49. return;
  50. }
  51. ShowMessageBus.ShowBinding.HistoryImageNum -= 1;
  52. UpDateShowBinding();
  53. }
  54. private void NextBtn_Click(object sender, RoutedEventArgs e)
  55. {
  56. if (ShowMessageBus.ShowBinding.HistoryImageNum >= ShowMessageBus.ShowBinding.HistoryImageCount)
  57. {
  58. MessageBox.Show("已经是最后一页");
  59. return;
  60. }
  61. ShowMessageBus.ShowBinding.HistoryImageNum += 1;
  62. UpDateShowBinding();
  63. }
  64. private void UpDateShowBinding()
  65. {
  66. Stopwatch stopwatch = Stopwatch.StartNew();
  67. BitmapImage ShowBitMap = null;
  68. Bitmap BitmapImage = null;
  69. List<ActiveObjectClass> ShowResult = actionMesSqliteDataClass.GetActiveObjectForPage((ShowMessageBus.ShowBinding.HistoryImageNum - 1) * PageHeight + DataStartLine, ShowMessageBus.ShowBinding.HistoryImageNum * PageHeight + DataStartLine);
  70. if (ShowResult.Count > 0)
  71. {
  72. int ThisImageStartLine = (int)ShowResult.Min(o => o.StartLine);
  73. int ImageHeight = (int)(ShowResult.Max(o => o.LastSeenLine) - ShowResult.Min(o => o.StartLine));
  74. int ImageWidth = ShowResult.Max(o => o.ImageWidth);
  75. BitmapImage = new Bitmap(ImageWidth, ImageHeight <= PageHeight ? PageHeight : ImageHeight);
  76. using (Graphics g = Graphics.FromImage(BitmapImage))
  77. {
  78. g.Clear(System.Drawing.Color.White);
  79. var redPen = new System.Drawing.Pen(System.Drawing.Color.Red, 1);
  80. var bluePen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(0, 146, 255), 1);
  81. var GreenPen = new System.Drawing.Pen(System.Drawing.Color.SeaGreen, 5);
  82. foreach (var item in ShowResult)
  83. {
  84. int roix = item.MinStartCol - 5;
  85. int roiy = (int)(item.StartLine - ThisImageStartLine) - 5;
  86. int roiheight = (int)(item.LastSeenLine - (long)item.StartLine) + 10;
  87. int roiwidth = item.MaxEndCol - item.MinStartCol + 10;
  88. g.DrawRectangle(GreenPen, new System.Drawing.Rectangle(roix, roiy, roiwidth, roiheight));
  89. Font font = new Font("Arial", 20);
  90. g.DrawString(item.Num.ToString(), font, System.Drawing.Brushes.Black, new System.Drawing.Point(roix - 20 * item.Num.ToString().Length, roiy - 20));
  91. foreach (var item1 in item.RowsData)
  92. {
  93. int yPos = (int)(item1.RowsCol - ThisImageStartLine);
  94. g.DrawLine(item.StateCode == 0 ? bluePen : redPen, new System.Drawing.Point(item1.StartCol, yPos), new System.Drawing.Point(item1.EndCol, yPos));
  95. }
  96. }
  97. }
  98. }
  99. else
  100. {
  101. BitmapImage = new Bitmap(4096, PageHeight);
  102. using (Graphics g = Graphics.FromImage(BitmapImage))
  103. {
  104. g.Clear(System.Drawing.Color.White);
  105. }
  106. }
  107. Application.Current.Dispatcher.InvokeAsync(() =>
  108. {
  109. ShowBitMap = ConvertToBitmapImage(BitmapImage);
  110. ShowMessageBus.ShowBinding.HistoryImage = ShowBitMap;
  111. });
  112. stopwatch.Stop();
  113. Console.WriteLine($"{stopwatch.ElapsedMilliseconds}ms");
  114. }
  115. // Bitmap 转 BitmapImage 的辅助方法
  116. private BitmapImage ConvertToBitmapImage(Bitmap bitmap)
  117. {
  118. using (MemoryStream memory = new MemoryStream())
  119. {
  120. bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
  121. memory.Position = 0;
  122. var bitmapImage = new BitmapImage();
  123. bitmapImage.BeginInit();
  124. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  125. bitmapImage.StreamSource = memory;
  126. bitmapImage.EndInit();
  127. return bitmapImage;
  128. }
  129. }
  130. private void SkipBtn_Click(object sender, RoutedEventArgs e)
  131. {
  132. UpDateShowBinding();
  133. }
  134. }
  135. }