SmudgeTreatmentDialog.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. using OpenCvSharp;
  2. using PaintDotNet.Annotation.Enum;
  3. using PaintDotNet.Base;
  4. using PaintDotNet.CustomControl;
  5. using PaintDotNet.Data.Param;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Drawing;
  9. using System.Windows.Forms;
  10. namespace PaintDotNet.Instrument
  11. {
  12. internal class SmudgeTreatmentDialog : FloatingToolForm/*PdnBaseForm*/
  13. {
  14. private AppWorkspace AppWorkspace;
  15. /// <summary>
  16. /// 当前处理的程序
  17. /// </summary>
  18. private ParamObject action;
  19. /// <summary>
  20. /// 原图片备份
  21. /// </summary>
  22. private Mat oldmat;
  23. //DrawSmudgeRectangle, //污迹处理-矩形 1
  24. //DrawSmudgePolygon, //污迹处理-多边形 2
  25. //DrawSmudgeCircle, //污迹处理-圆形 3
  26. //DrawSmudgeEllipse, //污迹处理-椭圆 4
  27. /// <summary>
  28. /// 将 操作类型的集合放入一个List,通过OperationIndex读取当前操作位置
  29. /// </summary>
  30. private List<int> OperationKtypeList = new List<int>();
  31. /// <summary>
  32. /// 实现撤销的功能,将处理因子、轮廓X Y W H、或折线上点X Y的集合放入一个List
  33. /// </summary>
  34. private List<double[]> OperationsList = new List<double[]>();
  35. //处理因子
  36. double inpaintRadius;
  37. #region 控件相关
  38. private Button button3;
  39. private Button button4;
  40. public Button button5;
  41. /// <summary>
  42. /// 操作区控件
  43. /// </summary>
  44. private GroupBox groupBox1;
  45. private GroupBox groupBox2;
  46. private Label label1;
  47. private Label label2;
  48. private RadioButton checkBox1;
  49. private RadioButton checkBox2;
  50. private RadioButton checkBox3;
  51. private RadioButton checkBox4;
  52. private UserTextBox textBox1;
  53. private CustomControl.SelectButton s12Button;
  54. private CustomControl.SelectButton s9Button;
  55. private CustomControl.SelectButton s6Button;
  56. private CustomControl.SelectButton logButton;
  57. private List<SelectButton> listButton = new List<SelectButton>();
  58. private void InitializeLanguageText()
  59. {
  60. this.button3.Text = PdnResources.GetString("Menu.application.text");
  61. this.button4.Text = PdnResources.GetString("Menu.ensure.text");
  62. this.button5.Text = "撤销";// PdnResources.GetString("Menu.cancel.text");
  63. this.label1.Text = PdnResources.GetString("Menu.tool.Stainprocessing.Shapesettings.text") + ":";
  64. this.label2.Text = PdnResources.GetString("Menu.tool.Stainprocessing.Processingfactor.text") + ":";
  65. this.checkBox1.Text = PdnResources.GetString("Menu.LabelAction.DrawRectangle.Text");
  66. this.checkBox2.Text = PdnResources.GetString("Menu.LabelAction.DrawPolygon.Text");
  67. this.checkBox3.Text = PdnResources.GetString("Menu.circular.Text");
  68. this.checkBox4.Text = PdnResources.GetString("Menu.LabelAction.DrawEllipse.Text");
  69. this.listButton.Add(this.s12Button);
  70. this.listButton.Add(this.s9Button);
  71. this.listButton.Add(this.s6Button);
  72. this.listButton.Add(this.logButton);
  73. }
  74. private void InitializeComponent()
  75. {
  76. this.groupBox1 = new System.Windows.Forms.GroupBox();
  77. this.label1 = new System.Windows.Forms.Label();
  78. this.checkBox1 = new System.Windows.Forms.RadioButton();
  79. this.checkBox2 = new System.Windows.Forms.RadioButton();
  80. this.checkBox3 = new System.Windows.Forms.RadioButton();
  81. this.checkBox4 = new System.Windows.Forms.RadioButton();
  82. this.groupBox2 = new System.Windows.Forms.GroupBox();
  83. this.label2 = new System.Windows.Forms.Label();
  84. this.logButton = new PaintDotNet.CustomControl.SelectButton();
  85. this.s6Button = new PaintDotNet.CustomControl.SelectButton();
  86. this.s9Button = new PaintDotNet.CustomControl.SelectButton();
  87. this.s12Button = new PaintDotNet.CustomControl.SelectButton();
  88. this.textBox1 = new PaintDotNet.CustomControl.UserTextBox();
  89. this.button3 = new System.Windows.Forms.Button();
  90. this.button4 = new System.Windows.Forms.Button();
  91. this.button5 = new System.Windows.Forms.Button();
  92. this.groupBox1.SuspendLayout();
  93. this.groupBox2.SuspendLayout();
  94. this.SuspendLayout();
  95. //
  96. // groupBox1
  97. //
  98. this.groupBox1.Controls.Add(this.label1);
  99. this.groupBox1.Controls.Add(this.checkBox1);
  100. this.groupBox1.Controls.Add(this.checkBox2);
  101. this.groupBox1.Controls.Add(this.checkBox3);
  102. this.groupBox1.Controls.Add(this.checkBox4);
  103. this.groupBox1.Location = new System.Drawing.Point(12, 6);
  104. this.groupBox1.Name = "groupBox1";
  105. this.groupBox1.Size = new System.Drawing.Size(301, 74);
  106. this.groupBox1.TabIndex = 3;
  107. this.groupBox1.TabStop = false;
  108. //
  109. // label1
  110. //
  111. this.label1.AutoSize = true;
  112. this.label1.Location = new System.Drawing.Point(12, 18);
  113. this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
  114. this.label1.Name = "label1";
  115. this.label1.Size = new System.Drawing.Size(65, 12);
  116. this.label1.TabIndex = 9;
  117. this.label1.Text = "形状设置:";
  118. //
  119. // checkBox1
  120. //
  121. this.checkBox1.Anchor = System.Windows.Forms.AnchorStyles.Left;
  122. this.checkBox1.AutoSize = true;
  123. this.checkBox1.Checked = true;
  124. this.checkBox1.Location = new System.Drawing.Point(108, 17);
  125. this.checkBox1.Name = "checkBox1";
  126. this.checkBox1.Size = new System.Drawing.Size(47, 16);
  127. this.checkBox1.TabIndex = 5;
  128. this.checkBox1.TabStop = true;
  129. this.checkBox1.Text = "矩形";
  130. this.checkBox1.UseVisualStyleBackColor = true;
  131. this.checkBox1.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
  132. //
  133. // checkBox2
  134. //
  135. this.checkBox2.Anchor = System.Windows.Forms.AnchorStyles.Left;
  136. this.checkBox2.AutoSize = true;
  137. this.checkBox2.Location = new System.Drawing.Point(204, 17);
  138. this.checkBox2.Name = "checkBox2";
  139. this.checkBox2.Size = new System.Drawing.Size(59, 16);
  140. this.checkBox2.TabIndex = 16;
  141. this.checkBox2.Text = "多边形";
  142. this.checkBox2.UseVisualStyleBackColor = true;
  143. this.checkBox2.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
  144. //
  145. // checkBox3
  146. //
  147. this.checkBox3.Anchor = System.Windows.Forms.AnchorStyles.Left;
  148. this.checkBox3.AutoSize = true;
  149. this.checkBox3.Location = new System.Drawing.Point(108, 51);
  150. this.checkBox3.Name = "checkBox3";
  151. this.checkBox3.Size = new System.Drawing.Size(47, 16);
  152. this.checkBox3.TabIndex = 17;
  153. this.checkBox3.Text = "圆形";
  154. this.checkBox3.UseVisualStyleBackColor = true;
  155. this.checkBox3.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
  156. //
  157. // checkBox4
  158. //
  159. this.checkBox4.Anchor = System.Windows.Forms.AnchorStyles.Left;
  160. this.checkBox4.AutoSize = true;
  161. this.checkBox4.Location = new System.Drawing.Point(204, 51);
  162. this.checkBox4.Name = "checkBox4";
  163. this.checkBox4.Size = new System.Drawing.Size(47, 16);
  164. this.checkBox4.TabIndex = 18;
  165. this.checkBox4.Text = "椭圆";
  166. this.checkBox4.UseVisualStyleBackColor = true;
  167. this.checkBox4.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
  168. //
  169. // groupBox2
  170. //
  171. this.groupBox2.Controls.Add(this.label2);
  172. this.groupBox2.Controls.Add(this.logButton);
  173. this.groupBox2.Controls.Add(this.s6Button);
  174. this.groupBox2.Controls.Add(this.s9Button);
  175. this.groupBox2.Controls.Add(this.s12Button);
  176. this.groupBox2.Controls.Add(this.textBox1);
  177. this.groupBox2.Location = new System.Drawing.Point(12, 80);
  178. this.groupBox2.Name = "groupBox2";
  179. this.groupBox2.Size = new System.Drawing.Size(301, 80);
  180. this.groupBox2.TabIndex = 4;
  181. this.groupBox2.TabStop = false;
  182. //
  183. // label2
  184. //
  185. this.label2.AutoSize = true;
  186. this.label2.Location = new System.Drawing.Point(12, 24);
  187. this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
  188. this.label2.Name = "label2";
  189. this.label2.Size = new System.Drawing.Size(65, 12);
  190. this.label2.TabIndex = 10;
  191. this.label2.Text = "处理因子:";
  192. //
  193. // logButton
  194. //
  195. this.logButton.BackColor = System.Drawing.SystemColors.ControlDark;
  196. this.logButton.BtnSelect = false;
  197. this.logButton.BtnText = "3";
  198. this.logButton.Location = new System.Drawing.Point(84, 50);
  199. this.logButton.Name = "logButton";
  200. this.logButton.Size = new System.Drawing.Size(31, 21);
  201. this.logButton.TabIndex = 21;
  202. this.logButton.Click += new System.EventHandler(this.skipButton_Click);
  203. //
  204. // s6Button
  205. //
  206. this.s6Button.BackColor = System.Drawing.SystemColors.ControlDark;
  207. this.s6Button.BtnSelect = false;
  208. this.s6Button.BtnText = "6";
  209. this.s6Button.Location = new System.Drawing.Point(140, 50);
  210. this.s6Button.Name = "s6Button";
  211. this.s6Button.Size = new System.Drawing.Size(31, 21);
  212. this.s6Button.TabIndex = 20;
  213. this.s6Button.Click += new System.EventHandler(this.skipButton_Click);
  214. //
  215. // s9Button
  216. //
  217. this.s9Button.BackColor = System.Drawing.SystemColors.ControlDark;
  218. this.s9Button.BtnSelect = false;
  219. this.s9Button.BtnText = "9";
  220. this.s9Button.Location = new System.Drawing.Point(196, 50);
  221. this.s9Button.Name = "s9Button";
  222. this.s9Button.Size = new System.Drawing.Size(31, 21);
  223. this.s9Button.TabIndex = 30;
  224. this.s9Button.Click += new System.EventHandler(this.skipButton_Click);
  225. //
  226. // s12Button
  227. //
  228. this.s12Button.BackColor = System.Drawing.SystemColors.ControlDark;
  229. this.s12Button.BtnSelect = false;
  230. this.s12Button.BtnText = "12";
  231. this.s12Button.Location = new System.Drawing.Point(252, 50);
  232. this.s12Button.Name = "s12Button";
  233. this.s12Button.Size = new System.Drawing.Size(31, 21);
  234. this.s12Button.TabIndex = 31;
  235. this.s12Button.Click += new System.EventHandler(this.skipButton_Click);
  236. //
  237. // textBox1
  238. //
  239. this.textBox1.Location = new System.Drawing.Point(84, 18);
  240. this.textBox1.Name = "textBox1";
  241. this.textBox1.Size = new System.Drawing.Size(199, 21);
  242. this.textBox1.TabIndex = 4;
  243. //
  244. // button3
  245. //
  246. this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  247. this.button3.Location = new System.Drawing.Point(240, 173);
  248. this.button3.Name = "button3";
  249. this.button3.Size = new System.Drawing.Size(65, 23);
  250. this.button3.TabIndex = 5;
  251. this.button3.Text = "应用";
  252. this.button3.UseVisualStyleBackColor = true;
  253. this.button3.Click += new System.EventHandler(this.button3_Click);
  254. //
  255. // button4
  256. //
  257. this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  258. this.button4.Location = new System.Drawing.Point(100, 173);
  259. this.button4.Name = "button4";
  260. this.button4.Size = new System.Drawing.Size(65, 23);
  261. this.button4.TabIndex = 6;
  262. this.button4.Text = "确定";
  263. this.button4.UseVisualStyleBackColor = true;
  264. this.button4.Click += new System.EventHandler(this.button4_Click);
  265. //
  266. // button5
  267. //
  268. this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  269. this.button5.Location = new System.Drawing.Point(170, 173);
  270. this.button5.Name = "button5";
  271. this.button5.Size = new System.Drawing.Size(65, 23);
  272. this.button5.TabIndex = 7;
  273. this.button5.Text = "撤销";
  274. this.button5.UseVisualStyleBackColor = true;
  275. this.button5.Click += new System.EventHandler(this.button5_Click);
  276. //
  277. // SmudgeTreatmentDialog
  278. //
  279. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  280. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  281. this.ClientSize = new System.Drawing.Size(325, 208);
  282. this.Controls.Add(this.groupBox2);
  283. this.Controls.Add(this.groupBox1);
  284. this.Controls.Add(this.button5);
  285. this.Controls.Add(this.button4);
  286. this.Controls.Add(this.button3);
  287. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  288. this.Margin = new System.Windows.Forms.Padding(6);
  289. this.Name = "SmudgeTreatmentDialog";
  290. this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.SmudgeTreatmentDialog_FormClosing);
  291. this.Controls.SetChildIndex(this.button3, 0);
  292. this.Controls.SetChildIndex(this.button4, 0);
  293. this.Controls.SetChildIndex(this.button5, 0);
  294. this.Controls.SetChildIndex(this.groupBox1, 0);
  295. this.Controls.SetChildIndex(this.groupBox2, 0);
  296. this.groupBox1.ResumeLayout(false);
  297. this.groupBox1.PerformLayout();
  298. this.groupBox2.ResumeLayout(false);
  299. this.groupBox2.PerformLayout();
  300. this.ResumeLayout(false);
  301. }
  302. #endregion
  303. public SmudgeTreatmentDialog(AppWorkspace AppWorkspace)
  304. {
  305. this.AppWorkspace = AppWorkspace;
  306. this.ShowInTaskbar = false;
  307. InitializeComponent();
  308. InitializeLanguageText();
  309. this.Text = PdnResources.GetString("Menu.Tools.SmudgeTreatment.Text");/*;//待修改中文资源-scc备注 20200709*/
  310. action = new Data.Action.Action1215();
  311. this.AppWorkspace.ActiveDocumentWorkspaceChanging += new EventHandler(AppWorkspace_ActiveDocumentWorkspaceChanging);
  312. this.AppWorkspace.ActiveDocumentWorkspaceChanged += new EventHandler(this.ActiveDocumentWorkspaceChanged);
  313. this.VisibleChanged += new EventHandler(this.SmudgeTreatmentDialog_FormClosed);
  314. }
  315. private void SmudgeTreatmentDialog_FormClosed(object sender, EventArgs/*FormClosedEventArgs*/ e)
  316. {
  317. if (this.Visible)
  318. return;
  319. if (AppWorkspace.ActiveDocumentWorkspace != null)
  320. {
  321. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgeRectangle);
  322. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgeCircle);//#18267
  323. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgeEllipse);
  324. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgePolygon);
  325. if (AppWorkspace.ActiveDocumentWorkspace.AppWorkspace != null)
  326. AppWorkspace.ActiveDocumentWorkspace.AppWorkspace.RefreshLabelListDialog();
  327. AppWorkspace.ActiveDocumentWorkspace.Refresh();
  328. }
  329. if (AppWorkspace.ActiveDocumentWorkspace != null)
  330. {
  331. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.Pointer;
  332. }
  333. }
  334. private void ActiveDocumentWorkspaceChanged(object sender, EventArgs e)
  335. {
  336. if (!this.Visible)
  337. return;
  338. RefreshActiveTool();
  339. }
  340. public void RefreshActiveTool()
  341. {
  342. if (this.AppWorkspace.ActiveDocumentWorkspace != null)
  343. {
  344. if (this.checkBox1.Checked)
  345. {//修改ActiveTool的 时候用大写ActiveTool
  346. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgeRectangle;// DrawSmudgePolygon;// DrawSmudgeEllipse;//
  347. }
  348. else if (this.checkBox2.Checked)
  349. {
  350. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgePolygon;// DrawSmudgeEllipse;//
  351. }
  352. else if (this.checkBox3.Checked)
  353. {
  354. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgeCircle;
  355. }
  356. else if (this.checkBox4.Checked)
  357. {
  358. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgeEllipse;
  359. }
  360. }
  361. //this.PhaseModelsBackUp = this.AppWorkspace.ActiveDocumentWorkspace.PhaseModelsForCopy;
  362. }
  363. /// <summary>
  364. /// 图片切换的处理方法 //#20193
  365. /// </summary>
  366. /// <param name="sender"></param>
  367. /// <param name="e"></param>
  368. private void AppWorkspace_ActiveDocumentWorkspaceChanging(object sender, EventArgs e)
  369. {
  370. if (!this.Visible)
  371. return;
  372. if (oldmat != null)
  373. {
  374. if (MessageBox.Show("是否保存当前图片的处理结果" + "?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
  375. {
  376. if (oldmat != null)
  377. {
  378. ////Document doc = Document.FromMat(oldmat);
  379. ////AppWorkspace.ActiveDocumentWorkspace.Document = doc;
  380. //AppWorkspace.ActiveDocumentWorkspace.Refresh();
  381. oldmat.Dispose();
  382. oldmat = null;
  383. }
  384. }
  385. else
  386. {
  387. Document doc = Document.FromMat(oldmat.Clone());
  388. AppWorkspace.ActiveDocumentWorkspace.Document = doc;
  389. AppWorkspace.ActiveDocumentWorkspace/*this.documentWorkspace*/.Refresh();
  390. oldmat.Dispose();
  391. oldmat = null;
  392. }
  393. }
  394. if (AppWorkspace.ActiveDocumentWorkspace != null)
  395. {
  396. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgeRectangle);
  397. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgeCircle);//#18267
  398. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgeEllipse);
  399. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgePolygon);
  400. if (AppWorkspace.ActiveDocumentWorkspace.AppWorkspace != null)
  401. AppWorkspace.ActiveDocumentWorkspace.AppWorkspace.RefreshLabelListDialog();
  402. AppWorkspace.ActiveDocumentWorkspace.Refresh();
  403. }
  404. if (AppWorkspace.ActiveDocumentWorkspace != null)
  405. {
  406. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.Pointer;
  407. }
  408. //this.Close();
  409. }
  410. private bool toApplyMethod()
  411. {
  412. if (this.AppWorkspace.ActiveDocumentWorkspace == null)
  413. {
  414. MessageBox.Show(PdnResources.GetString("Menu.Pleaseopenthepicture.Text"));
  415. return false;
  416. }
  417. //处理因子
  418. if (double.TryParse(textBox1.Text, out inpaintRadius))
  419. {
  420. if (inpaintRadius < 1)
  421. {
  422. MessageBox.Show(PdnResources.GetString("Menu.leasenumbergreatethanforprocessingfac.Text"));
  423. return false;
  424. }
  425. }
  426. else
  427. {
  428. MessageBox.Show(PdnResources.GetString("Menu.leasenumbergreatethanforprocessingfac.Text"));
  429. return false;
  430. }
  431. return true;
  432. }
  433. /// <summary>
  434. /// 应用按钮点击方法
  435. /// </summary>
  436. /// <param name="sender"></param>
  437. /// <param name="e"></param>
  438. private void button3_Click(object sender, EventArgs e)
  439. {
  440. if (oldmat != null)
  441. {
  442. //Document doc = Document.FromMat(oldmat);
  443. //AppWorkspace.ActiveDocumentWorkspace.Document = doc;
  444. AppWorkspace.ActiveDocumentWorkspace.Refresh();
  445. oldmat.Dispose();
  446. oldmat = null;
  447. }
  448. this.Close();
  449. //if (!toApplyMethod())
  450. //{
  451. // return;
  452. //}
  453. //this.button4_Click(sender, e);
  454. //this.Close();
  455. }
  456. /// <summary>
  457. /// 确定按钮点击方法
  458. /// </summary>
  459. /// <param name="sender"></param>
  460. /// <param name="e"></param>
  461. private void button4_Click(object sender, EventArgs e)
  462. {
  463. if (!toApplyMethod())
  464. {
  465. return;
  466. }
  467. //读取参数信息
  468. for (int i = 0; i < action.lists.Count; i++)
  469. {
  470. Base.Args args = action.lists[i];
  471. switch (args.Key)
  472. {
  473. case "InpaintRadius":
  474. args.Value = inpaintRadius + "";
  475. break;
  476. }
  477. }
  478. PaintDotNet.Annotation.ISurfaceBox drawArea = AppWorkspace.ActiveDocumentWorkspace;
  479. if (drawArea != null && drawArea.GraphicsList != null && drawArea.GraphicsList.Count > 0)
  480. {
  481. for (int i = drawArea.GraphicsList.Count - 1; i >= 0; i--)
  482. {
  483. if (drawArea.GraphicsList[i].drawToolType == DrawToolType.DrawSmudgeRectangle/*DrawSmudgePolygon*//*DrawScratchTreatmentLine*//*drawArea.GraphicsList[i].Selected == true*/)
  484. {
  485. applyImp1(drawArea.GraphicsList[i].Rectangle);
  486. break;
  487. //drawArea.GraphicsList[i].GetPoints();
  488. ////drawArea.GraphicsList.RemoveObj(drawArea.GraphicsList[i]);
  489. }
  490. else if (drawArea.GraphicsList[i].drawToolType == DrawToolType.DrawSmudgePolygon/*DrawScratchTreatmentLine*//*drawArea.GraphicsList[i].Selected == true*/)
  491. {
  492. applyImp2(drawArea.GraphicsList[i].GetPoints());
  493. break;
  494. //drawArea.GraphicsList[i].GetPoints();
  495. ////drawArea.GraphicsList.RemoveObj(drawArea.GraphicsList[i]);
  496. }
  497. else if (drawArea.GraphicsList[i].drawToolType == DrawToolType.DrawSmudgeCircle)
  498. {
  499. applyImp3(drawArea.GraphicsList[i].Rectangle);
  500. break;
  501. }
  502. else if (drawArea.GraphicsList[i].drawToolType == DrawToolType.DrawSmudgeEllipse)
  503. {
  504. applyImp4(drawArea.GraphicsList[i].Rectangle);
  505. break;
  506. }
  507. }
  508. //drawArea.Refresh();
  509. }
  510. }
  511. protected override void WndProc(ref Message m)
  512. {
  513. if (m.Msg == 0x0112/*WM_SYSCOMMAND*/)
  514. {
  515. if (m.WParam.ToInt32() == 0xf060/*NativeConstants.SC_CLOSE*/) //是否点击关闭
  516. {
  517. //关闭时发生的事件
  518. if (oldmat != null)
  519. {
  520. Document doc = Document.FromMat(oldmat.Clone());
  521. AppWorkspace.ActiveDocumentWorkspace.Document = doc;
  522. AppWorkspace.ActiveDocumentWorkspace/*this.documentWorkspace*/.Refresh();
  523. oldmat.Dispose();
  524. oldmat = null;
  525. }
  526. }
  527. }
  528. base.WndProc(ref m);
  529. }
  530. /// <summary>
  531. /// 撤销按钮点击方法
  532. /// </summary>
  533. /// <param name="sender"></param>
  534. /// <param name="e"></param>
  535. private void button5_Click(object sender, EventArgs e)
  536. {
  537. if (oldmat != null && OperationsList.Count > 0 && OperationKtypeList.Count == OperationsList.Count)
  538. {
  539. OperationsList.RemoveAt(OperationsList.Count - 1);
  540. OperationKtypeList.RemoveAt(OperationKtypeList.Count - 1);
  541. Mat matUndo = oldmat;
  542. for (int i = 0; i < OperationsList.Count; i++)
  543. {
  544. double[] opeItem = OperationsList[i];
  545. double inpaintRadiusUndo = opeItem[0];
  546. //读取参数信息
  547. for (int iact = 0; iact < action.lists.Count; iact++)
  548. {
  549. Base.Args args = action.lists[iact];
  550. switch (args.Key)
  551. {
  552. case "InpaintRadius":
  553. args.Value = inpaintRadiusUndo + "";
  554. break;
  555. }
  556. }
  557. int opeKtype = OperationKtypeList[i];
  558. if (opeKtype == 1)
  559. {
  560. Mat imageMaskUndo = new Mat(oldmat.Size(), MatType.CV_8UC1, new Scalar(0));
  561. OpenCvSharp.Point[][] arr = new OpenCvSharp.Point[1][];
  562. arr[0] = new OpenCvSharp.Point[] { new OpenCvSharp.Point(opeItem[1], opeItem[2]), new OpenCvSharp.Point((int)(opeItem[1] + opeItem[3]), opeItem[2]), new OpenCvSharp.Point((int)(opeItem[1]+ opeItem[3]), (int)(opeItem[2]+ opeItem[4])), new OpenCvSharp.Point(opeItem[1], opeItem[2]+ opeItem[4]) };
  563. Cv2.FillPoly(imageMaskUndo, arr, new Scalar(255));
  564. matUndo = action.PerformMaskCal(i == 0 ? oldmat : matUndo, imageMaskUndo);
  565. if (imageMaskUndo != null) imageMaskUndo.Dispose();
  566. GC.Collect();
  567. }
  568. else if (opeKtype == 2)
  569. {
  570. Mat imageMask = new Mat(oldmat.Size(), MatType.CV_8UC1, new Scalar(0));
  571. OpenCvSharp.Point[][] arr = new OpenCvSharp.Point[1][];
  572. List<PointF> pointArray = new List<PointF>();
  573. for (int iope = 1; iope < opeItem.Length - 1; iope+=2)
  574. {
  575. pointArray.Add(new PointF((float)opeItem[iope], (float)opeItem[iope + 1]));
  576. }
  577. arr[0] = Adjust.BaseImage.BaseTools.pointToPoint(pointArray/*points*/).ToArray();
  578. Cv2.FillPoly(imageMask, arr/*ps*//*pointArray.ToArray()*/, new Scalar(255));
  579. matUndo = action.PerformMaskCal(i == 0 ? oldmat : matUndo, imageMask);
  580. if (imageMask != null) imageMask.Dispose();
  581. GC.Collect();
  582. }
  583. else if (opeKtype == 3)
  584. {
  585. float radius = (float)((opeItem[3]) < (opeItem[4]) ? (opeItem[3]) : (opeItem[4])) / 2;
  586. Mat imageMask = new Mat(oldmat.Size(), MatType.CV_8UC1, new Scalar(0));
  587. Cv2.Circle(imageMask, (int)(opeItem[1] + radius), (int)(opeItem[2] + radius), (int)radius, new Scalar(255), -1);
  588. matUndo = action.PerformMaskCal(i == 0 ? oldmat : matUndo, imageMask);
  589. if (imageMask != null) imageMask.Dispose();
  590. GC.Collect();
  591. }
  592. else if (opeKtype == 4)
  593. {
  594. Mat imageMask = new Mat(oldmat.Size(), MatType.CV_8UC1, new Scalar(0));
  595. Cv2.Ellipse(imageMask, new RotatedRect(new Point2f((float)(opeItem[1] + opeItem[1] + opeItem[3]) / 2, (float)(opeItem[2] + opeItem[2] + opeItem[4]) / 2), new Size2f(opeItem[3], opeItem[4]), 0), new Scalar(255), -1);
  596. matUndo = action.PerformMaskCal(i == 0 ? oldmat : matUndo, imageMask);
  597. if (imageMask != null) imageMask.Dispose();
  598. GC.Collect();
  599. }
  600. }
  601. if (OperationsList.Count == 0)
  602. matUndo = oldmat.Clone();
  603. Document doc = Document.FromMat(matUndo);
  604. AppWorkspace.ActiveDocumentWorkspace.Document = doc;
  605. ////移除
  606. //AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgeRectangle);
  607. //刷新
  608. AppWorkspace.ActiveDocumentWorkspace.Refresh();
  609. //AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgeRectangle;
  610. this.button5.Enabled = OperationsList.Count > 0;
  611. }
  612. else
  613. this.button5.Enabled = false;
  614. //if (oldmat != null)
  615. //{
  616. // Document doc = Document.FromMat(oldmat);
  617. // AppWorkspace.ActiveDocumentWorkspace.Document = doc;
  618. // AppWorkspace.ActiveDocumentWorkspace/*this.documentWorkspace*/.Refresh();
  619. // oldmat.Dispose();
  620. // oldmat = null;
  621. //}
  622. //this.Close();
  623. }
  624. /// <summary>
  625. /// check radio改变事件
  626. /// </summary>
  627. /// <param name="sender"></param>
  628. /// <param name="e"></param>
  629. private void radioButton_CheckedChanged(object sender, EventArgs e)
  630. {
  631. //清空界面现有污迹处理标记
  632. if (AppWorkspace.ActiveDocumentWorkspace != null)
  633. {
  634. /*AppWorkspace.ActiveDocumentWorkspace.tools[(int)DrawToolType.DrawSmudgeRectangle].OnDel2KeyDown(AppWorkspace.ActiveDocumentWorkspace, null);
  635. AppWorkspace.ActiveDocumentWorkspace.tools[(int)DrawToolType.DrawSmudgePolygon].OnDel2KeyDown(AppWorkspace.ActiveDocumentWorkspace, null);
  636. AppWorkspace.ActiveDocumentWorkspace.tools[(int)DrawToolType.DrawSmudgeCircle].OnDel2KeyDown(AppWorkspace.ActiveDocumentWorkspace, null);
  637. AppWorkspace.ActiveDocumentWorkspace.tools[(int)DrawToolType.DrawSmudgeEllipse].OnDel2KeyDown(AppWorkspace.ActiveDocumentWorkspace, null);*/
  638. if (AppWorkspace.ActiveDocumentWorkspace.AppWorkspace != null)
  639. AppWorkspace.ActiveDocumentWorkspace.AppWorkspace.RefreshLabelListDialog();
  640. if (this.checkBox1.Checked)
  641. {//修改ActiveTool的 时候用大写ActiveTool
  642. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgeRectangle;// DrawSmudgePolygon;// DrawSmudgeEllipse;//
  643. }
  644. else if (this.checkBox2.Checked)
  645. {
  646. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgePolygon;// DrawSmudgeEllipse;//
  647. }
  648. else if (this.checkBox3.Checked)
  649. {
  650. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgeCircle;
  651. }
  652. else if (this.checkBox4.Checked)
  653. {
  654. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgeEllipse;
  655. }
  656. }
  657. ////还原时发生的事件
  658. //if (oldmat != null)
  659. //{
  660. // Document doc = Document.FromMat(oldmat);
  661. // AppWorkspace.ActiveDocumentWorkspace.Document = doc;
  662. // AppWorkspace.ActiveDocumentWorkspace/*this.documentWorkspace*/.Refresh();
  663. // oldmat.Dispose();
  664. // oldmat = null;
  665. //}
  666. }
  667. /// <summary>
  668. /// 应用方法实现-矩形
  669. /// </summary>
  670. /// <param name="startP"></param>
  671. /// <param name="endP"></param>
  672. private void applyImp1(RectangleF rectangle, bool undoAction = false)
  673. {
  674. if (action.MenuId == ActionType.SmudgeTreatment)//划痕处理
  675. {
  676. if (oldmat == null)
  677. oldmat = AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat().Clone();
  678. OperationKtypeList.Add(1);
  679. OperationsList.Add(new double[] { inpaintRadius, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height });
  680. this.button5.Enabled = OperationsList.Count > 0;
  681. Mat imageMask = new Mat(oldmat.Size(), MatType.CV_8UC1, new Scalar(0));
  682. OpenCvSharp.Point[][] arr = new OpenCvSharp.Point[1][];
  683. arr[0] = new OpenCvSharp.Point[] { new OpenCvSharp.Point(rectangle.X, rectangle.Y), new OpenCvSharp.Point(rectangle.X + rectangle.Width, rectangle.Y), new OpenCvSharp.Point(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height), new OpenCvSharp.Point(rectangle.X, rectangle.Y + rectangle.Height) };
  684. Cv2.FillPoly(imageMask, arr, new Scalar(255));
  685. Mat calMat = AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat().Clone();
  686. Mat mat = action.PerformMaskCal(calMat, imageMask);
  687. Document doc = Document.FromMat(mat);
  688. AppWorkspace.ActiveDocumentWorkspace.Document = doc;
  689. //移除
  690. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgeRectangle);
  691. //刷新
  692. AppWorkspace.ActiveDocumentWorkspace.Refresh();
  693. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgeRectangle;
  694. if (imageMask != null) imageMask.Dispose();
  695. //if (mat != null) mat.Dispose();
  696. GC.Collect();
  697. }
  698. }
  699. /// <summary>
  700. /// 应用方法实现-多边形
  701. /// </summary>
  702. /// <param name="startP"></param>
  703. /// <param name="endP"></param>
  704. private void applyImp2(List<PointF> pointArray, bool undoAction = false)
  705. {
  706. if (action.MenuId == ActionType.SmudgeTreatment)//划痕处理
  707. {
  708. if (oldmat == null)
  709. oldmat = AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat().Clone();
  710. OperationKtypeList.Add(2);
  711. List<double> itemList = new List<double>();
  712. itemList.Add(inpaintRadius);
  713. foreach (PointF item in pointArray)
  714. {
  715. itemList.Add(item.X);
  716. itemList.Add(item.Y);
  717. }
  718. OperationsList.Add(itemList.ToArray());
  719. this.button5.Enabled = OperationsList.Count > 0;
  720. Mat imageMask = new Mat(oldmat.Size(), MatType.CV_8UC1, new Scalar(0));
  721. OpenCvSharp.Point[][] arr = new OpenCvSharp.Point[1][];
  722. arr[0] = Adjust.BaseImage.BaseTools.pointToPoint(pointArray/*points*/).ToArray();
  723. Cv2.FillPoly(imageMask, arr/*ps*//*pointArray.ToArray()*/, new Scalar(255));
  724. Mat calMat = AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat().Clone();
  725. Mat mat = action.PerformMaskCal(calMat, imageMask);
  726. //Cv2.ImShow("test", mat);
  727. Document doc = Document.FromMat(mat);
  728. AppWorkspace.ActiveDocumentWorkspace.Document = doc;
  729. //移除
  730. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgePolygon);
  731. //刷新
  732. AppWorkspace.ActiveDocumentWorkspace.Refresh();
  733. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgePolygon;
  734. if (imageMask != null) imageMask.Dispose();
  735. //if (mat != null) mat.Dispose();
  736. GC.Collect();
  737. }
  738. }
  739. /// <summary>
  740. /// 应用方法实现-圆形
  741. /// </summary>
  742. /// <param name="startP"></param>
  743. /// <param name="endP"></param>
  744. private void applyImp3(RectangleF rectangle, bool undoAction = false)
  745. {
  746. if (action.MenuId == ActionType.SmudgeTreatment)//划痕处理
  747. {
  748. if (oldmat == null)
  749. oldmat = AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat().Clone();
  750. float x1 = rectangle.X;
  751. float y1 = rectangle.Y;
  752. float x2 = rectangle.Right;
  753. float y2 = rectangle.Bottom;
  754. if (x2 < x1)
  755. {
  756. float tmp = x2;
  757. x2 = x1;
  758. x1 = tmp;
  759. }
  760. if (y2 < y1)
  761. {
  762. float tmp = y2;
  763. y2 = y1;
  764. y1 = tmp;
  765. }
  766. OperationKtypeList.Add(3);
  767. OperationsList.Add(new double[] { inpaintRadius, x1, y1, x2 - x1, y2 - y1 });
  768. this.button5.Enabled = OperationsList.Count > 0;
  769. float radius = ((x2 - x1) < (y2 - y1) ? (x2 - x1) : (y2 - y1)) / 2;
  770. Mat imageMask = new Mat(oldmat.Size(), MatType.CV_8UC1, new Scalar(0));
  771. Cv2.Circle(imageMask, (int)(x1 + radius), (int)(y1 + radius), (int)radius, new Scalar(255), -1);
  772. Mat calMat = AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat().Clone();
  773. Mat mat = action.PerformMaskCal(calMat, imageMask);
  774. Document doc = Document.FromMat(mat/*.Clone()*/);
  775. AppWorkspace.ActiveDocumentWorkspace.Document = doc;
  776. //移除
  777. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgeCircle);
  778. //刷新
  779. AppWorkspace.ActiveDocumentWorkspace.Refresh();
  780. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgeCircle;
  781. if (imageMask != null) imageMask.Dispose();
  782. //if (mat != null) mat.Dispose();
  783. GC.Collect();
  784. }
  785. }
  786. /// <summary>
  787. /// 应用方法实现-椭圆
  788. /// </summary>
  789. /// <param name="startP"></param>
  790. /// <param name="endP"></param>
  791. private void applyImp4(RectangleF rectangle, bool undoAction = false)
  792. {
  793. if (action.MenuId == ActionType.SmudgeTreatment)//划痕处理
  794. {
  795. if (oldmat == null)
  796. oldmat = AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat().Clone();
  797. float x1 = rectangle.X;
  798. float y1 = rectangle.Y;
  799. float x2 = rectangle.Right;
  800. float y2 = rectangle.Bottom;
  801. if (x2 < x1)
  802. {
  803. float tmp = x2;
  804. x2 = x1;
  805. x1 = tmp;
  806. }
  807. if (y2 < y1)
  808. {
  809. float tmp = y2;
  810. y2 = y1;
  811. y1 = tmp;
  812. }
  813. OperationKtypeList.Add(4);
  814. OperationsList.Add(new double[] { inpaintRadius, x1, y1, x2 - x1, y2 - y1 });
  815. this.button5.Enabled = OperationsList.Count > 0;
  816. Mat imageMask = new Mat(oldmat.Size(), MatType.CV_8UC1, new Scalar(0));
  817. Cv2.Ellipse(imageMask, new RotatedRect(new Point2f((x1 + x2) / 2, (y1 + y2) / 2), new Size2f(x2 - x1, y2 - y1), 0), new Scalar(255), -1);
  818. //Cv2.Circle(imageMask, (int)(x1 + radius), (int)(y1 + radius), (int)radius, new Scalar(255), -1);
  819. Mat calMat = AppWorkspace.ActiveDocumentWorkspace.CompositionSurface.CreatedAliasedMat().Clone();
  820. Mat mat = action.PerformMaskCal(calMat, imageMask);
  821. Document doc = Document.FromMat(mat);
  822. AppWorkspace.ActiveDocumentWorkspace.Document = doc;
  823. //移除
  824. AppWorkspace.ActiveDocumentWorkspace.GraphicsList.DeleteDrawClass(DrawToolType.DrawSmudgeEllipse);
  825. //刷新
  826. AppWorkspace.ActiveDocumentWorkspace.Refresh();
  827. AppWorkspace.ActiveDocumentWorkspace.ActiveTool = DrawToolType.DrawSmudgeEllipse;
  828. if (imageMask != null) imageMask.Dispose();
  829. //if (mat != null) mat.Dispose();
  830. GC.Collect();
  831. }
  832. }
  833. /// <summary>
  834. /// skip按钮
  835. /// 显示直方图时,忽略黑色的灰度或颜色值。
  836. /// 这使您可以为背景为黑色的图像实现有意义的直方图显示。
  837. /// </summary>
  838. /// <param name="sender"></param>
  839. /// <param name="e"></param>
  840. private void skipButton_Click(object sender, EventArgs e)
  841. {
  842. //if (this.AppWorkspace.ActiveDocumentWorkspace == null)
  843. //{
  844. // MessageBox.Show("请打开图片");
  845. // return;
  846. //}
  847. foreach (SelectButton selectBtn in this.listButton)
  848. {
  849. if (selectBtn == sender)
  850. {
  851. //设置按钮的选中/非选择的状态
  852. s6Button.BtnSelect = false;// true;
  853. }
  854. else
  855. {
  856. s6Button.BtnSelect = false;
  857. }
  858. this.textBox1.Text = ((SelectButton)sender).BtnText;
  859. }
  860. ////设置按钮的选中/非选择的状态
  861. //s6Button.BtnSelect = !s6Button.BtnSelect;
  862. //this.AppWorkspace.ActiveDocumentWorkspace.HistogramSkipEnabled = s6Button.BtnSelect;
  863. }
  864. private void SmudgeTreatmentDialog_FormClosing(object sender, FormClosingEventArgs e)
  865. {
  866. #region [开启脚本录制]
  867. if (AppWorkspace.startScriptRecording)
  868. {
  869. getScriptRecording();
  870. }
  871. #endregion
  872. this.AppWorkspace.toolBar.RefreshBtnSelect(false, "SmudgeTreatment");
  873. this.AppWorkspace.toolsPanel.RefreshBtnSelect(false, "SmudgeTreatment");
  874. }
  875. #region [脚本录制]
  876. private void getScriptRecording()
  877. {
  878. List<Args> args = action.Lists;
  879. int menuId = action.MenuId == ActionType.SmudgeTreatment ? 1215 : 1214;
  880. string text = menuId == 1215 ? "污迹处理" : "划痕处理";
  881. this.AppWorkspace.SetScriptStartRecording(menuId, text, args);
  882. }
  883. #endregion
  884. }
  885. }