OpticalDensityDialog.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using OpenCvSharp;
  14. using PaintDotNet.Annotation;
  15. using PaintDotNet.Annotation.Enum;
  16. using PaintDotNet.Annotation.Measure;
  17. using PaintDotNet.Annotation.relationModel;
  18. using PaintDotNet.Base.CommTool;
  19. using static PaintDotNet.Annotation.relationModel.MeasureRelationModel;
  20. using Point = System.Drawing.Point;
  21. /// <summary>
  22. /// 光密度
  23. /// </summary>
  24. namespace PaintDotNet.Measuring
  25. {
  26. partial class OpticalDensityDialog : FloatingToolForm
  27. {
  28. #region 控件
  29. /// Required designer variable.
  30. /// </summary>
  31. private System.ComponentModel.IContainer components = null;
  32. /// <summary>
  33. /// Clean up any resources being used.
  34. /// </summary>
  35. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  36. protected override void Dispose(bool disposing)
  37. {
  38. if (disposing && (components != null))
  39. {
  40. components.Dispose();
  41. }
  42. base.Dispose(disposing);
  43. }
  44. #region Windows Form Designer generated code
  45. private void InitializeLanguageText()
  46. {
  47. this.button1.Text = PdnResources.GetString("Menu.measuring.text");
  48. this.button2.Text = PdnResources.GetString("Menu.Imagement.Opticaldensity.Dataoutput.text");
  49. this.button3.Text = PdnResources.GetString("Menu.Imagement.Opticaldensity.imageexport.text");
  50. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  51. this.groupBox2.Text = PdnResources.GetString("Menu.display.text");
  52. this.Text = PdnResources.GetString("Menu.MeasureAction.OpticalDensity.Text");
  53. }
  54. /// <summary>
  55. /// Required method for Designer support - do not modify
  56. /// the contents of this method with the code editor.
  57. /// </summary>
  58. private void InitializeComponent()
  59. {
  60. this.button1 = new System.Windows.Forms.Button();
  61. this.button2 = new System.Windows.Forms.Button();
  62. this.button3 = new System.Windows.Forms.Button();
  63. this.groupBox1 = new System.Windows.Forms.GroupBox();
  64. this.panel1 = new PaintDotNet.PanelEx();
  65. this.groupBox2 = new System.Windows.Forms.GroupBox();
  66. this.groupBox1.SuspendLayout();
  67. this.groupBox2.SuspendLayout();
  68. this.SuspendLayout();
  69. //
  70. // button1
  71. //
  72. this.button1.Location = new System.Drawing.Point(161, 29);
  73. this.button1.Name = "button1";
  74. this.button1.Size = new System.Drawing.Size(75, 23);
  75. this.button1.TabIndex = 4;
  76. this.button1.Text = "测量";
  77. this.button1.UseVisualStyleBackColor = true;
  78. this.button1.Click += new System.EventHandler(this.button1_Click);
  79. //
  80. // button2
  81. //
  82. this.button2.Location = new System.Drawing.Point(243, 29);
  83. this.button2.Name = "button2";
  84. this.button2.Size = new System.Drawing.Size(75, 23);
  85. this.button2.TabIndex = 5;
  86. this.button2.Text = "数据导出";
  87. this.button2.UseVisualStyleBackColor = true;
  88. this.button2.Click += new System.EventHandler(this.button2_Click);
  89. //
  90. // button3
  91. //
  92. this.button3.Location = new System.Drawing.Point(325, 29);
  93. this.button3.Name = "button3";
  94. this.button3.Size = new System.Drawing.Size(75, 23);
  95. this.button3.TabIndex = 6;
  96. this.button3.Text = "图片导出";
  97. this.button3.UseVisualStyleBackColor = true;
  98. this.button3.Click += new System.EventHandler(this.button3_Click);
  99. //
  100. // groupBox1
  101. //
  102. this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  103. | System.Windows.Forms.AnchorStyles.Right)));
  104. this.groupBox1.Controls.Add(this.button1);
  105. this.groupBox1.Controls.Add(this.button2);
  106. this.groupBox1.Controls.Add(this.button3);
  107. this.groupBox1.Location = new System.Drawing.Point(27, 13);
  108. this.groupBox1.Name = "groupBox1";
  109. this.groupBox1.Size = new System.Drawing.Size(420, 75);
  110. this.groupBox1.TabIndex = 4;
  111. this.groupBox1.TabStop = false;
  112. this.groupBox1.Text = "操作";
  113. //
  114. // panel1
  115. //
  116. this.panel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
  117. this.panel1.BackColor = System.Drawing.SystemColors.ControlLightLight;
  118. this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
  119. this.panel1.HideHScroll = false;
  120. this.panel1.HideVScroll = false;
  121. this.panel1.IgnoreSetFocus = false;
  122. this.panel1.Location = new System.Drawing.Point(3, 17);
  123. this.panel1.Name = "panel1";
  124. this.panel1.ScrollPosition = new System.Drawing.Point(0, 0);
  125. this.panel1.Size = new System.Drawing.Size(414, 315);
  126. this.panel1.TabIndex = 0;
  127. this.panel1.TabStop = true;
  128. this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint_1);
  129. this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown_1);
  130. this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove_1);
  131. this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseUp);
  132. //
  133. // groupBox2
  134. //
  135. this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  136. | System.Windows.Forms.AnchorStyles.Left)
  137. | System.Windows.Forms.AnchorStyles.Right)));
  138. this.groupBox2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
  139. this.groupBox2.BackColor = System.Drawing.SystemColors.Control;
  140. this.groupBox2.Controls.Add(this.panel1);
  141. this.groupBox2.Location = new System.Drawing.Point(27, 94);
  142. this.groupBox2.Name = "groupBox2";
  143. this.groupBox2.Size = new System.Drawing.Size(420, 335);
  144. this.groupBox2.TabIndex = 5;
  145. this.groupBox2.TabStop = false;
  146. this.groupBox2.Text = "显示";
  147. //
  148. // OpticalDensityDialog
  149. //
  150. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  151. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  152. this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
  153. this.BackColor = System.Drawing.SystemColors.Control;
  154. this.ClientSize = new System.Drawing.Size(471, 441);
  155. this.Controls.Add(this.groupBox2);
  156. this.Controls.Add(this.groupBox1);
  157. this.MaximumSize = new System.Drawing.Size(487, 480);
  158. this.MinimumSize = new System.Drawing.Size(487, 480);
  159. this.Name = "OpticalDensityDialog";
  160. this.Text = "光密度";
  161. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.OpticalDensityDialog_FormClosing);
  162. this.Controls.SetChildIndex(this.groupBox1, 0);
  163. this.Controls.SetChildIndex(this.groupBox2, 0);
  164. this.groupBox1.ResumeLayout(false);
  165. this.groupBox2.ResumeLayout(false);
  166. this.ResumeLayout(false);
  167. }
  168. #endregion
  169. private System.Windows.Forms.Button button1;
  170. private System.Windows.Forms.Button button2;
  171. private System.Windows.Forms.Button button3;
  172. private System.Windows.Forms.GroupBox groupBox1;
  173. private PanelEx panel1;
  174. private System.Windows.Forms.GroupBox groupBox2;
  175. #endregion
  176. /// <summary>
  177. /// 主控件
  178. /// </summary>
  179. private AppWorkspace appWorkspace;
  180. /// <summary>
  181. /// 关系数据
  182. /// </summary>
  183. private static List<MeasureRelationModel> list;
  184. /// <summary>
  185. /// 横坐标比例
  186. /// </summary>
  187. private float proportion;
  188. /// <summary>
  189. /// 灰度值点数
  190. /// </summary>
  191. private int pointNumber;
  192. private Rectangle sRect;
  193. private PointF StartPoint = new Point(70, 10);
  194. private Rectangle eRect;
  195. private PointF EndPoint = new Point(350, 10);
  196. Pen pe = new Pen(Color.YellowGreen, 4);
  197. private bool sCanMove;
  198. private bool eCanMove;
  199. private PointF startPoint;
  200. private PointF endPoint;
  201. private string unitString;
  202. private string unit;
  203. /// <summary>
  204. /// 步长 初始值1,依次增加
  205. /// </summary>
  206. private string stepLength = "00" + Startup.instance.step_length;
  207. public OpticalDensityDialog(AppWorkspace appWorkspace)
  208. {
  209. if (list == null)
  210. {
  211. list = InvariantData.GetMeasureRelations();
  212. }
  213. this.appWorkspace = appWorkspace;
  214. InitializeComponent();
  215. InitializeLanguageText();
  216. }
  217. /// <summary>
  218. /// 刷新光密度画面
  219. /// </summary>
  220. /// <param name="sender"></param>
  221. /// <param name="e"></param>
  222. public void RefreshOpticalDensity(object sender, EventArgs e)
  223. {
  224. this.panel1.Refresh();
  225. }
  226. /// <summary>
  227. /// 绘制绿线
  228. /// </summary>
  229. /// <param name="graphics"></param>
  230. private void drawRect(Graphics graphics)
  231. {
  232. graphics.DrawLine(pe, EndPoint, new PointF(EndPoint.X, EndPoint.Y + 260));
  233. graphics.DrawLine(pe, StartPoint, new PointF(StartPoint.X, StartPoint.Y + 260));
  234. }
  235. private void means(Graphics graphics)
  236. {
  237. sRect = new Rectangle((int)StartPoint.X, (int)StartPoint.Y, 5, 260);
  238. eRect = new Rectangle((int)EndPoint.X, (int)EndPoint.Y, 5, 260);
  239. StartPoint = sRect.Location;
  240. EndPoint = eRect.Location;
  241. //graphics.DrawLine(pe, new Point((int)StartPoint.X, 270), new Point((int)StartPoint.X, 10));
  242. //graphics.DrawLine(pe, new Point((int)EndPoint.X, 270), new Point((int)EndPoint.X, 10));
  243. }
  244. /// <summary>
  245. /// 绘制截线
  246. /// </summary>
  247. private void DrawInMainScreen()
  248. {
  249. double kS = (StartPoint.X - 70) / 280;
  250. double kE = (350 - EndPoint.X) / 280;
  251. if (this.appWorkspace.ActiveDocumentWorkspace != null)
  252. this.DrawInMainScreen(kS, kE);
  253. }
  254. public void DrawInMainScreen(double kS, double kE)
  255. {
  256. //循环所有测量
  257. if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList != null
  258. && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count > 0)
  259. {
  260. int count = this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count;
  261. for (int i = 0; i < count; i++)
  262. {
  263. if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].objectType == DrawClass.Other)
  264. {
  265. ((MeasureDrawObject)(this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i])).kS = kS;
  266. ((MeasureDrawObject)(this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i])).kE = kE;
  267. }
  268. }
  269. }
  270. this.appWorkspace.ActiveDocumentWorkspace.Refresh();
  271. }
  272. /// <summary>
  273. /// 绘制刻度
  274. /// </summary>
  275. private void Setscale(Graphics graphics)
  276. {
  277. Font textfont = new Font(Font, FontStyle.Regular);
  278. Brush textbrush = new SolidBrush(Color.Black);
  279. graphics.DrawLine(new Pen(Color.Black, 2), new Point(70, 270), new Point(350, 270));
  280. graphics.DrawLine(new Pen(Color.Black, 2), new Point(70, 270), new Point(70, 10));
  281. // 纵坐标刻度
  282. graphics.DrawLine(new Pen(Color.Black, 2), new Point(63, 270), new Point(70, 270));
  283. graphics.DrawLine(new Pen(Color.Black, 2), new Point(63, 220), new Point(70, 220));
  284. graphics.DrawLine(new Pen(Color.Black, 2), new Point(63, 170), new Point(350, 170));
  285. graphics.DrawLine(new Pen(Color.Black, 2), new Point(63, 120), new Point(70, 120));
  286. graphics.DrawLine(new Pen(Color.Black, 2), new Point(63, 70), new Point(350, 70));
  287. graphics.DrawLine(new Pen(Color.Black, 2), new Point(63, 20), new Point(70, 20));
  288. graphics.DrawString("0", textfont, textbrush, new Point(42, 263));
  289. graphics.DrawString("100", textfont, textbrush, new Point(42, 163));
  290. graphics.DrawString("200", textfont, textbrush, new Point(42, 63));
  291. graphics.DrawString(PdnResources.GetString("Menu.MeasureAction.MeasureDistanceLine.Text") +"(" + unitString + ")", textfont, textbrush, new Point(180, 300));
  292. graphics.DrawString(PdnResources.GetString("Menu.Imagement.Opticaldensity.Linecontour.text"), textfont, textbrush, new Point(180, 0));
  293. graphics.DrawString(PdnResources.GetString("Menu.strong.Text") + "\r\n" + " " + "\r\n" + PdnResources.GetString("Menu.degree.Text"), textfont, textbrush, new Point(20, 5));
  294. // 横坐标刻度
  295. int n;
  296. int growth = 70;
  297. int scale = 0;
  298. graphics.DrawString(scale.ToString(), textfont, textbrush, new Point(65, 280));
  299. if (this.pointNumber <= 200)
  300. {
  301. n = this.pointNumber / 10;
  302. for (int i = 0; i < n; i++)
  303. {
  304. scale = scale + 10;
  305. growth = growth + 280 / n;
  306. if ((i + 1) % 2 == 0)
  307. {
  308. graphics.DrawLine(new Pen(Color.Black, 2), new Point(growth, 10), new Point(growth, 277));
  309. graphics.DrawString(scale.ToString(), textfont, textbrush, new Point(growth, 277));
  310. }
  311. else
  312. {
  313. graphics.DrawLine(new Pen(Color.Black, 2), new Point(growth, 270), new Point(growth, 277));
  314. }
  315. }
  316. }
  317. else if (this.pointNumber > 200 && this.pointNumber <= 1000)
  318. {
  319. n = this.pointNumber / 100;
  320. for (int i = 0; i < n; i++)
  321. {
  322. scale = scale + 50;
  323. growth = growth + 280 / n;
  324. if ((i + 1) % 2 == 0)
  325. {
  326. graphics.DrawLine(new Pen(Color.Black, 2), new Point(growth, 10), new Point(growth, 277));
  327. graphics.DrawString(scale.ToString(), textfont, textbrush, new Point(growth, 277));
  328. }
  329. else
  330. {
  331. graphics.DrawLine(new Pen(Color.Black, 2), new Point(growth, 270), new Point(growth, 277));
  332. }
  333. }
  334. }
  335. else if (this.pointNumber > 1000)
  336. {
  337. int length1 = Convert.ToInt32(BasicCalculationHelper.GetDistance(startPoint, endPoint, 2));
  338. int f = 280 / 50;
  339. for (int i = 0; i < f; i++)
  340. {
  341. int scale1 = (int)(1000 * i * 50 / 280);
  342. //if ((i + 1) % 2 == 0)
  343. {
  344. //graphics.DrawLine(new Pen(Color.Black, 2), new Point(growth, 10), new Point(growth, 277));
  345. graphics.DrawString(scale1.ToString(), textfont, textbrush, new Point(50 * i + 70, 277));
  346. }
  347. //else
  348. {
  349. //graphics.DrawLine(new Pen(Color.Black, 2), new Point(growth, 270), new Point(growth, 277));
  350. }
  351. }
  352. /* n = this.pointNumber / 200;
  353. for (int i = 0; i < n; i++)
  354. {
  355. scale = scale + 100;
  356. growth = growth + 280 / n;
  357. if ((i + 1) % 2 == 0)
  358. {
  359. graphics.DrawLine(new Pen(Color.Black, 2), new Point(growth, 10), new Point(growth, 277));
  360. graphics.DrawString(scale.ToString(), textfont, textbrush, new Point(growth, 277));
  361. }
  362. else
  363. {
  364. graphics.DrawLine(new Pen(Color.Black, 2), new Point(growth, 270), new Point(growth, 277));
  365. }
  366. }*/
  367. }
  368. }
  369. /// <summary>
  370. /// 绘制灰度图像
  371. /// </summary>
  372. private void DrawGrayScale(Graphics graphics)
  373. {
  374. if (this.appWorkspace.ActiveDocumentWorkspace != null
  375. && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList != null
  376. && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count > 0)
  377. {
  378. int count = this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count;
  379. for (int i = 0; i < count; i++)
  380. {
  381. if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].objectType == DrawClass.Other)
  382. {
  383. if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].drawToolType == DrawToolType.OpticalDensityLine)
  384. {
  385. startPoint = this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].startPoint;
  386. endPoint = this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].endPoint;
  387. OpenCvSharp.Mat mat = PaintDotNet.Camera.Tools.ToMat(
  388. this.appWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreateAliasedBitmap());
  389. //定义线上的点
  390. List<Point> linePoint = new List<Point>();
  391. Color color = Color.Blue;
  392. Pen pen = new Pen(color, 1);
  393. double k = 0;
  394. double y = 0;
  395. double x = 0;
  396. if (startPoint.X != endPoint.X)
  397. {
  398. // 计算斜率
  399. k = ((double)(startPoint.Y - endPoint.Y)) / (startPoint.X - endPoint.X);
  400. // 循环x坐标
  401. double maxPointX;
  402. double minPointX;
  403. if (startPoint.X > endPoint.X)
  404. {
  405. maxPointX = startPoint.X;
  406. minPointX = endPoint.X;
  407. }
  408. else
  409. {
  410. maxPointX = endPoint.X;
  411. minPointX = startPoint.X;
  412. }
  413. if (Math.Abs(startPoint.X - endPoint.X) > Math.Abs(startPoint.Y - endPoint.Y))
  414. {
  415. for (int j = (int)(minPointX) + 1; j < maxPointX; j++)
  416. {
  417. if (k == 0 && startPoint.Y == endPoint.Y)
  418. {
  419. y = startPoint.Y;
  420. }
  421. else
  422. {
  423. // 根据斜率,计算y坐标
  424. y = k * (j - startPoint.X) + startPoint.Y;
  425. }
  426. linePoint.Add(new Point(j, (int)y));
  427. }
  428. }
  429. else
  430. {
  431. if (startPoint.Y > endPoint.Y)
  432. {
  433. maxPointX = startPoint.Y;
  434. minPointX = endPoint.Y;
  435. }
  436. else
  437. {
  438. maxPointX = endPoint.Y;
  439. minPointX = startPoint.Y;
  440. }
  441. for (int j = (int)(minPointX) + 1; j < maxPointX; j++)
  442. {
  443. if (k == 0 && startPoint.Y == endPoint.Y)
  444. {
  445. y = startPoint.Y;
  446. }
  447. else
  448. {
  449. // 根据斜率,计算y坐标
  450. x = -((startPoint.Y - j) / k - startPoint.X);
  451. }
  452. linePoint.Add(new Point((int)x, j));
  453. }
  454. }
  455. }
  456. else
  457. {
  458. double maxPointY;
  459. double minPointY;
  460. if (startPoint.Y > endPoint.Y)
  461. {
  462. maxPointY = startPoint.Y;
  463. minPointY = endPoint.Y;
  464. }
  465. else
  466. {
  467. maxPointY = endPoint.Y;
  468. minPointY = startPoint.Y;
  469. }
  470. // 循环y坐标
  471. for (int j = (int)(minPointY) + 1; j < maxPointY; j++)
  472. {
  473. // 根据斜率,计算y坐标
  474. y = j;
  475. linePoint.Add(new Point((int)startPoint.X, (int)y));
  476. }
  477. }
  478. // 灰度值数据
  479. List<int> list0 = new List<int>();
  480. List<int> list1 = new List<int>();
  481. List<int> list2 = new List<int>();
  482. foreach (var point in linePoint)
  483. {
  484. if(point.X < mat.Width && point.Y < mat.Height && point.Y >= 0 && point.X >= 0)
  485. {
  486. Vec3b vec3B = new Vec3b();
  487. byte a;
  488. if (mat.Type() == MatType.CV_8UC1)
  489. {
  490. a = mat.At<byte>(point.Y, point.X);
  491. list0.Add(a);
  492. }
  493. else
  494. {
  495. vec3B = mat.At<Vec3b>(point.Y, point.X);
  496. list0.Add(vec3B.Item0);
  497. list1.Add(vec3B.Item1);
  498. list2.Add(vec3B.Item2);
  499. }
  500. }
  501. }
  502. // 灰度图绘制坐标点
  503. List<PointF> pointList = new List<PointF>();
  504. List<PointF> pointList1 = new List<PointF>();
  505. List<PointF> pointList2 = new List<PointF>();
  506. this.pointNumber = list0.Count;
  507. this.proportion = 280 / (float)list0.Count;
  508. float px;
  509. if (mat.Type() == MatType.CV_8UC1)
  510. {
  511. px = 70;
  512. foreach (var item in list0)
  513. {
  514. pointList.Add(new PointF(px, 270 - item));
  515. px = px + this.proportion;
  516. }
  517. if (pointList.Count > 0)
  518. {
  519. color = Color.Blue;
  520. pen = new Pen(color, 1);
  521. graphics.DrawLines(pen, pointList.ToArray());
  522. }
  523. }
  524. else
  525. {
  526. px = 70;
  527. foreach (var item in list0)
  528. {
  529. pointList.Add(new PointF(px, 270 - item));
  530. px = px + this.proportion;
  531. }
  532. if (pointList.Count > 0)
  533. {
  534. color = Color.Blue;
  535. pen = new Pen(color, 1);
  536. graphics.DrawLines(pen, pointList.ToArray());
  537. }
  538. px = 70;
  539. foreach (var item in list1)
  540. {
  541. pointList1.Add(new PointF(px, 270 - item));
  542. px = px + this.proportion;
  543. }
  544. if (pointList1.Count > 0)
  545. {
  546. color = Color.Green;
  547. pen = new Pen(color, 1);
  548. graphics.DrawLines(pen, pointList1.ToArray());
  549. }
  550. px = 70;
  551. foreach (var item in list2)
  552. {
  553. pointList2.Add(new PointF(px, 270 - item));
  554. px = px + this.proportion;
  555. }
  556. if (pointList2.Count > 0)
  557. {
  558. color = Color.Red;
  559. pen = new Pen(color, 1);
  560. graphics.DrawLines(pen, pointList2.ToArray());
  561. }
  562. }
  563. pen.Dispose();
  564. }
  565. }
  566. }
  567. }
  568. }
  569. private void ExportEcxel()
  570. {
  571. string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + PdnResources.GetString("Menu.pticaldensitydata.Text");
  572. SaveFileDialog exe = new SaveFileDialog();
  573. exe.Filter = "Execl files (*.xlsx)|*.xlsx";
  574. exe.FilterIndex = 0;
  575. exe.RestoreDirectory = true;
  576. //exe.CreatePrompt = true;
  577. exe.Title = "Export Excel File";
  578. exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  579. exe.FileName = fileName;
  580. DialogResult dr = exe.ShowDialog();
  581. if (dr != DialogResult.OK)
  582. {
  583. return;
  584. }
  585. double length = BasicCalculationHelper.GetDistance(this.startPoint, this.endPoint, 2);
  586. double length2 =Math.Round(length * (Math.Abs(this.StartPoint.X - this.EndPoint.X) / 280), 2);
  587. DataTable dtb = new DataTable();
  588. dtb.Columns.Add(PdnResources.GetString("Menu.Measuringpointdistance.Text"));
  589. dtb.Columns.Add(PdnResources.GetString("Menu.Measurinlinedistance.Text"));
  590. dtb.Columns.Add(PdnResources.GetString("Menu.unit.text"));
  591. DataRow dataRow = dtb.NewRow();
  592. dataRow[PdnResources.GetString("Menu.Measuringpointdistance.Text")] = (length * Convert.ToDouble(unit)).ToString();
  593. dataRow[PdnResources.GetString("Menu.Measurinlinedistance.Text")] = (length2 * Convert.ToDouble(unit)).ToString();
  594. dataRow[PdnResources.GetString("Menu.unit.text")] = unitString;
  595. dtb.Rows.Add(dataRow);
  596. List<DataTable> list = new List<DataTable>();
  597. list.Add(dtb);
  598. this.appWorkspace.ExportDataToExcelWithProgress(list, exe.FileName, false,false, true);
  599. }
  600. /// <summary>
  601. /// 测量按钮按下
  602. /// </summary>
  603. /// <param name="sender"></param>
  604. /// <param name="e"></param>
  605. private void button1_Click(object sender, EventArgs e)
  606. {
  607. if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count == 0)
  608. {
  609. if (appWorkspace.ActiveDocumentWorkspace != null)
  610. {
  611. appWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.OpticalDensityLine;
  612. }
  613. }
  614. else
  615. {
  616. bool other = false;
  617. int count = this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count;
  618. for (int i = 0; i < count; i++)
  619. {
  620. if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].objectType == DrawClass.Other
  621. && this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].drawToolType == DrawToolType.OpticalDensityLine)
  622. {
  623. other = true;
  624. this.button1.Tag = this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i];
  625. }
  626. }
  627. if(!other)
  628. {
  629. if (appWorkspace.ActiveDocumentWorkspace != null)
  630. {
  631. appWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.OpticalDensityLine;
  632. }
  633. }
  634. else
  635. {
  636. if (this.button1.Tag != null)
  637. {
  638. var senderGrid = (Button)sender;
  639. DrawObject dObject;
  640. this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.UnselectAll();
  641. dObject = (DrawObject)senderGrid.Tag;
  642. dObject.Selected = true;
  643. }
  644. }
  645. this.appWorkspace.ActiveDocumentWorkspace.Refresh();
  646. }
  647. this.panel1.Focus();
  648. }
  649. /// <summary>
  650. /// 数据导出按钮
  651. /// </summary>
  652. /// <param name="sender"></param>
  653. /// <param name="e"></param>
  654. private void button2_Click(object sender, EventArgs e)
  655. {
  656. this.ExportEcxel();
  657. }
  658. /// <summary>
  659. /// 绘制事件
  660. /// </summary>
  661. /// <param name="sender"></param>
  662. /// <param name="e"></param>
  663. private void panel1_Paint_1(object sender, PaintEventArgs e)
  664. {
  665. unitString = this.appWorkspace.GetPxPerUnit()[1];
  666. unit = this.appWorkspace.GetPxPerUnit()[3];
  667. this.DrawGrayScale(e.Graphics);
  668. this.Setscale(e.Graphics);
  669. this.DrawGrayScale(e.Graphics);
  670. this.means(e.Graphics);
  671. this.drawRect(e.Graphics);
  672. DrawInMainScreen();
  673. }
  674. /// <summary>
  675. /// 鼠标移动事件
  676. /// </summary>
  677. /// <param name="sender"></param>
  678. /// <param name="e"></param>
  679. private void panel1_MouseMove_1(object sender, MouseEventArgs e)
  680. {
  681. if (e.Button == MouseButtons.Left && (sCanMove == true || eCanMove == true))
  682. {
  683. if (sCanMove == true && eCanMove == true)
  684. eCanMove = false;
  685. if (sCanMove == true)
  686. sRect.Offset((int)(e.X - StartPoint.X), 0);
  687. if (eCanMove == true)
  688. eRect.Offset((int)(e.X - EndPoint.X), 0);
  689. if (sRect.X < 70)
  690. sRect.X = 70;
  691. if (sRect.X > 350)
  692. sRect.X = 350;
  693. if (eRect.X < 70)
  694. eRect.X = 70;
  695. if (eRect.X > 350)
  696. eRect.X = 350;
  697. StartPoint = new Point(sRect.X, sRect.Y);
  698. EndPoint = new Point(eRect.X, eRect.Y);
  699. this.panel1.Refresh();
  700. }
  701. }
  702. /// <summary>
  703. /// 鼠标按下事件
  704. /// </summary>
  705. /// <param name="sender"></param>
  706. /// <param name="e"></param>
  707. private void panel1_MouseDown_1(object sender, MouseEventArgs e)
  708. {
  709. if (sRect.Contains(e.Location))
  710. {
  711. sCanMove = true;
  712. }
  713. if (eRect.Contains(e.Location))
  714. {
  715. eCanMove = true;
  716. }
  717. }
  718. /// <summary>
  719. /// 鼠标抬起事件
  720. /// </summary>
  721. /// <param name="sender"></param>
  722. /// <param name="e"></param>
  723. private void panel1_MouseUp(object sender, MouseEventArgs e)
  724. {
  725. sCanMove = false;
  726. eCanMove = false;
  727. }
  728. private void button3_Click(object sender, EventArgs e)
  729. {
  730. Bitmap bmp = new Bitmap(panel1.Width, panel1.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
  731. panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height));
  732. DocumentWorkspace dw = appWorkspace.AddNewDocumentWorkspace();
  733. Document document = Document.FromImageMat(Camera.Tools.ToMat(bmp));
  734. dw.Document = document;
  735. dw.xmlSaveModel = Startup.instance.ruleDB;
  736. dw.InitRulerInfo();
  737. dw.fileText = this.Text + "-" + stepLength;
  738. appWorkspace.ActiveDocumentWorkspace = dw;
  739. Startup.instance.step_length += 1;
  740. stepLength = "00" + Startup.instance.step_length;
  741. }
  742. private void OpticalDensityDialog_FormClosing(object sender, FormClosingEventArgs e)
  743. {
  744. if (this.appWorkspace.ActiveDocumentWorkspace != null)
  745. {
  746. this.appWorkspace.toolBar.RefreshBtnSelect(false, "OpticalDensity");
  747. this.appWorkspace.toolsPanel.RefreshBtnSelect(false, "OpticalDensity");
  748. for (int i = 0; i < this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.Count; i++)
  749. {
  750. if (this.appWorkspace.ActiveDocumentWorkspace.GraphicsList[i].drawToolType == DrawToolType.OpticalDensityLine)
  751. {
  752. this.appWorkspace.ActiveDocumentWorkspace.GraphicsList.RemoveAt(i);
  753. this.appWorkspace.Refresh();
  754. }
  755. }
  756. }
  757. }
  758. }
  759. }