HistoryImageDataForm.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using CCDCount.DLL;
  2. using CCDCount.MODEL.ShuLiClass;
  3. using CsvHelper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Diagnostics;
  9. using System.Drawing;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace CCDCount.Forms
  17. {
  18. public partial class HistoryImageDataForm : Form
  19. {
  20. public List<ActiveObjectClass> historyActive = null;
  21. Stopwatch ShowImageStopwatch = new Stopwatch();
  22. int Frame = 24;
  23. string csvFilePath = "";
  24. StreamWriter writer = null;
  25. CsvWriter csv = null;
  26. string csvRowsFilePath = "";
  27. StreamWriter Rowswriter = null;
  28. CsvWriter Rowscsv = null;
  29. public HistoryImageDataForm()
  30. {
  31. InitializeComponent();
  32. }
  33. private void HistoryDataForm_Load(object sender, EventArgs e)
  34. {
  35. trackBar1.Maximum = historyActive.Count-1;
  36. ShowImageStopwatch.Start();
  37. PrintHistoryImage(0);
  38. }
  39. private void trackBar1_Scroll(object sender, EventArgs e)
  40. {
  41. if (ShowImageStopwatch.ElapsedMilliseconds < (1000 / Frame))
  42. {
  43. return;
  44. }
  45. else
  46. {
  47. ShowImageStopwatch.Restart();
  48. }
  49. PrintHistoryImage(trackBar1.Value);
  50. }
  51. private void PrintHistoryImage(int index)
  52. {
  53. GC.Collect();
  54. ActiveObjectClass activeObjectClass = historyActive[index];
  55. Bitmap bitmap = new Bitmap(activeObjectClass.ImageWidth, 2048);
  56. Graphics g = Graphics.FromImage(bitmap);
  57. Pen redPen = new Pen(Color.Red, 1);
  58. List<ActiveObjectClass> ShowList = historyActive.Where(o => o.LastSeenLine <= activeObjectClass.StartLine + 2048).ToList();
  59. for (int i = 0; i < ShowList.Count; i++)
  60. {
  61. ShowList[i].RowsData.ForEach(o => g.DrawLine(redPen, new Point(o.StartCol,bitmap.Height-(int)(o.RowsCol - activeObjectClass.StartLine)), new Point(o.EndCol, bitmap.Height - (int)(o.RowsCol - activeObjectClass.StartLine))));
  62. };
  63. UpdatepictureBox1(bitmap.Clone() as Bitmap);
  64. }
  65. private void UpdatepictureBox1(Bitmap image)
  66. {
  67. if (pictureBox1.InvokeRequired)
  68. {
  69. pictureBox1.Invoke(new Action<Bitmap>(UpdatepictureBox1), image);
  70. }
  71. else
  72. {
  73. pictureBox1.Image = image;
  74. }
  75. }
  76. private void button1_Click(object sender, EventArgs e)
  77. {
  78. csvFilePath = ".\\HistoryData\\" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".csv";
  79. csvRowsFilePath = ".\\HistoryData\\Rows" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".csv";
  80. if (!Directory.Exists(".\\HistoryData\\")) Directory.CreateDirectory(".\\HistoryData\\");
  81. writer = new StreamWriter(csvFilePath);
  82. csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
  83. csv.WriteHeader<ActiveObjectClass>();
  84. csv.NextRecord();
  85. Rowswriter = new StreamWriter(csvRowsFilePath);
  86. Rowscsv = new CsvWriter(Rowswriter, CultureInfo.InvariantCulture);
  87. Rowscsv.WriteHeader<RowStartEndCol>();
  88. Rowscsv.NextRecord();
  89. var batchRecords = historyActive.GetRange(0, historyActive.Count);
  90. csv.WriteRecords(batchRecords);
  91. csv.Flush(); // 确保数据写入文件
  92. for (int i = 0; i < historyActive.Count; i++)
  93. {
  94. List<RowStartEndCol> batchRecords2 = historyActive[i].RowsData.GetRange(0, historyActive[i].RowsData.Count);
  95. batchRecords2.Add(new RowStartEndCol());
  96. Rowscsv.WriteRecords(batchRecords2);
  97. Rowscsv.Flush(); // 确保数据写入文件
  98. }
  99. }
  100. }
  101. }