ImageComparison.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. using PaintDotNet.Processing;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.Linq;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace PaintDotNet.Instrument
  14. {
  15. internal class ImageComparison : PdnBaseForm
  16. {
  17. private GroupBox groupBox1;
  18. private Button button2;
  19. private GroupBox groupBox2;
  20. private GroupBox groupBox3;
  21. private GroupBox groupBox4;
  22. private Button button4;
  23. private Button button3;
  24. private Button button6;
  25. private Button button5;
  26. private PictureBox pictureBox1;
  27. private TrackBar trackBar1;
  28. private Button button7;
  29. private PictureBox pictureBox2;
  30. private TrackBar trackBar2;
  31. private Button button8;
  32. private Button button1;
  33. private PictureBox pictureBox3;
  34. /// <summary>
  35. /// 图片选择窗口
  36. /// </summary>
  37. private InterImagePickerDialog imagePickerDialog;
  38. /// <summary>
  39. /// 第一个小picturebox的原图
  40. /// </summary>
  41. private Bitmap bitmap1;
  42. /// <summary>
  43. /// 第二个小picturebox的原图
  44. /// </summary>
  45. private Bitmap bitmap2;
  46. /// <summary>
  47. /// 合并之后的图片
  48. /// </summary>
  49. private Bitmap combineBitmap;
  50. /// <summary>
  51. /// 合并时所使用的修改过透明度的图片数组
  52. /// </summary>
  53. private Bitmap[] bitArray = new Bitmap[2];
  54. /// <summary>
  55. /// 判断两张图片的绘制顺序
  56. /// </summary>
  57. private bool ordered = true;
  58. /// <summary>
  59. /// 文件名排序
  60. /// </summary>
  61. string stepLength = "00" + Startup.instance.step_length;
  62. /// <summary>
  63. /// 判断当前点击的按钮是哪个小picturebox的
  64. /// </summary>
  65. private int pictureBoxNum = 0;
  66. private AppWorkspace appWorkspace;
  67. public ImageComparison(AppWorkspace appWorkspace)
  68. {
  69. this.appWorkspace = appWorkspace;
  70. InitializeComponent();
  71. InitializeLanguageText();
  72. }
  73. private void InitializeLanguageText()
  74. {
  75. this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  76. this.button2.Text = PdnResources.GetString("Menu.Createpicture.text");
  77. this.button1.Text = PdnResources.GetString("Menu.File.Close.Text");
  78. this.groupBox2.Text = PdnResources.GetString("Menu.tool.imagecomparison.compareresults.text");
  79. this.groupBox3.Text = PdnResources.GetString("Menu.picture.Text");
  80. this.button4.Text = PdnResources.GetString("Menu.Gallery.text");
  81. this.button3.Text = PdnResources.GetString("Menu.harddisk.text");
  82. this.groupBox4.Text = PdnResources.GetString("Menu.picture.Text");
  83. this.button6.Text = PdnResources.GetString("Menu.Gallery.text");
  84. this.button5.Text = PdnResources.GetString("Menu.harddisk.text");
  85. this.Text = PdnResources.GetString("Menu.Tools.PictureComparison.Text");
  86. }
  87. private void InitializeComponent()
  88. {
  89. this.groupBox1 = new System.Windows.Forms.GroupBox();
  90. this.button2 = new System.Windows.Forms.Button();
  91. this.button1 = new System.Windows.Forms.Button();
  92. this.groupBox2 = new System.Windows.Forms.GroupBox();
  93. this.pictureBox3 = new System.Windows.Forms.PictureBox();
  94. this.groupBox3 = new System.Windows.Forms.GroupBox();
  95. this.button7 = new System.Windows.Forms.Button();
  96. this.trackBar1 = new System.Windows.Forms.TrackBar();
  97. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  98. this.button4 = new System.Windows.Forms.Button();
  99. this.button3 = new System.Windows.Forms.Button();
  100. this.groupBox4 = new System.Windows.Forms.GroupBox();
  101. this.button8 = new System.Windows.Forms.Button();
  102. this.trackBar2 = new System.Windows.Forms.TrackBar();
  103. this.pictureBox2 = new System.Windows.Forms.PictureBox();
  104. this.button6 = new System.Windows.Forms.Button();
  105. this.button5 = new System.Windows.Forms.Button();
  106. this.groupBox1.SuspendLayout();
  107. this.groupBox2.SuspendLayout();
  108. ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
  109. this.groupBox3.SuspendLayout();
  110. ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
  111. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  112. this.groupBox4.SuspendLayout();
  113. ((System.ComponentModel.ISupportInitialize)(this.trackBar2)).BeginInit();
  114. ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
  115. this.SuspendLayout();
  116. //
  117. // groupBox1
  118. //
  119. this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
  120. | System.Windows.Forms.AnchorStyles.Right)));
  121. this.groupBox1.Controls.Add(this.button2);
  122. this.groupBox1.Controls.Add(this.button1);
  123. this.groupBox1.Location = new System.Drawing.Point(13, 13);
  124. this.groupBox1.Name = "groupBox1";
  125. this.groupBox1.Size = new System.Drawing.Size(621, 55);
  126. this.groupBox1.TabIndex = 0;
  127. this.groupBox1.TabStop = false;
  128. this.groupBox1.Text = "操作";
  129. //
  130. // button2
  131. //
  132. this.button2.Anchor = System.Windows.Forms.AnchorStyles.Right;
  133. this.button2.Location = new System.Drawing.Point(426, 20);
  134. this.button2.Name = "button2";
  135. this.button2.Size = new System.Drawing.Size(75, 23);
  136. this.button2.TabIndex = 1;
  137. this.button2.Text = "创建图片";
  138. this.button2.UseVisualStyleBackColor = true;
  139. this.button2.Click += new System.EventHandler(this.button2_Click);
  140. //
  141. // button1
  142. //
  143. this.button1.Anchor = System.Windows.Forms.AnchorStyles.Right;
  144. this.button1.Location = new System.Drawing.Point(524, 20);
  145. this.button1.Name = "button1";
  146. this.button1.Size = new System.Drawing.Size(75, 23);
  147. this.button1.TabIndex = 0;
  148. this.button1.Text = "关闭";
  149. this.button1.UseVisualStyleBackColor = true;
  150. this.button1.Click += new System.EventHandler(this.button1_Click);
  151. //
  152. // groupBox2
  153. //
  154. this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  155. | System.Windows.Forms.AnchorStyles.Left)
  156. | System.Windows.Forms.AnchorStyles.Right)));
  157. this.groupBox2.Controls.Add(this.pictureBox3);
  158. this.groupBox2.Location = new System.Drawing.Point(13, 75);
  159. this.groupBox2.Name = "groupBox2";
  160. this.groupBox2.Size = new System.Drawing.Size(383, 469);
  161. this.groupBox2.TabIndex = 1;
  162. this.groupBox2.TabStop = false;
  163. this.groupBox2.Text = "对比结果";
  164. //
  165. // pictureBox3
  166. //
  167. this.pictureBox3.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  168. | System.Windows.Forms.AnchorStyles.Left)
  169. | System.Windows.Forms.AnchorStyles.Right)));
  170. this.pictureBox3.Location = new System.Drawing.Point(7, 21);
  171. this.pictureBox3.Name = "pictureBox3";
  172. this.pictureBox3.Size = new System.Drawing.Size(370, 434);
  173. this.pictureBox3.TabIndex = 0;
  174. this.pictureBox3.TabStop = false;
  175. //
  176. // groupBox3
  177. //
  178. this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  179. this.groupBox3.Controls.Add(this.button7);
  180. this.groupBox3.Controls.Add(this.trackBar1);
  181. this.groupBox3.Controls.Add(this.pictureBox1);
  182. this.groupBox3.Controls.Add(this.button4);
  183. this.groupBox3.Controls.Add(this.button3);
  184. this.groupBox3.Location = new System.Drawing.Point(402, 75);
  185. this.groupBox3.Name = "groupBox3";
  186. this.groupBox3.Size = new System.Drawing.Size(232, 232);
  187. this.groupBox3.TabIndex = 2;
  188. this.groupBox3.TabStop = false;
  189. this.groupBox3.Text = "图片";
  190. //
  191. // button7
  192. //
  193. this.button7.Location = new System.Drawing.Point(197, 181);
  194. this.button7.Name = "button7";
  195. this.button7.Size = new System.Drawing.Size(23, 23);
  196. this.button7.TabIndex = 4;
  197. this.button7.Text = "↑";
  198. this.button7.UseVisualStyleBackColor = true;
  199. this.button7.Click += new System.EventHandler(this.button7_Click);
  200. //
  201. // trackBar1
  202. //
  203. this.trackBar1.Cursor = System.Windows.Forms.Cursors.Hand;
  204. this.trackBar1.Location = new System.Drawing.Point(6, 181);
  205. this.trackBar1.Maximum = 255;
  206. this.trackBar1.Name = "trackBar1";
  207. this.trackBar1.Size = new System.Drawing.Size(185, 45);
  208. this.trackBar1.TabIndex = 3;
  209. this.trackBar1.TickStyle = System.Windows.Forms.TickStyle.None;
  210. this.trackBar1.Value = 125;
  211. this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
  212. //
  213. // pictureBox1
  214. //
  215. this.pictureBox1.Location = new System.Drawing.Point(7, 47);
  216. this.pictureBox1.Name = "pictureBox1";
  217. this.pictureBox1.Size = new System.Drawing.Size(219, 127);
  218. this.pictureBox1.TabIndex = 2;
  219. this.pictureBox1.TabStop = false;
  220. //
  221. // button4
  222. //
  223. this.button4.Location = new System.Drawing.Point(125, 20);
  224. this.button4.Name = "button4";
  225. this.button4.Size = new System.Drawing.Size(67, 21);
  226. this.button4.TabIndex = 1;
  227. this.button4.Text = "图库";
  228. this.button4.UseVisualStyleBackColor = true;
  229. this.button4.Click += new System.EventHandler(this.button4_Click);
  230. //
  231. // button3
  232. //
  233. this.button3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
  234. this.button3.Location = new System.Drawing.Point(52, 20);
  235. this.button3.Name = "button3";
  236. this.button3.Size = new System.Drawing.Size(67, 21);
  237. this.button3.TabIndex = 0;
  238. this.button3.Text = "硬盘";
  239. this.button3.UseVisualStyleBackColor = true;
  240. this.button3.Click += new System.EventHandler(this.button3_Click);
  241. //
  242. // groupBox4
  243. //
  244. this.groupBox4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
  245. this.groupBox4.Controls.Add(this.button8);
  246. this.groupBox4.Controls.Add(this.trackBar2);
  247. this.groupBox4.Controls.Add(this.pictureBox2);
  248. this.groupBox4.Controls.Add(this.button6);
  249. this.groupBox4.Controls.Add(this.button5);
  250. this.groupBox4.Location = new System.Drawing.Point(402, 313);
  251. this.groupBox4.Name = "groupBox4";
  252. this.groupBox4.Size = new System.Drawing.Size(231, 231);
  253. this.groupBox4.TabIndex = 3;
  254. this.groupBox4.TabStop = false;
  255. this.groupBox4.Text = "图片";
  256. //
  257. // button8
  258. //
  259. this.button8.Location = new System.Drawing.Point(197, 181);
  260. this.button8.Name = "button8";
  261. this.button8.Size = new System.Drawing.Size(23, 23);
  262. this.button8.TabIndex = 4;
  263. this.button8.Text = "↑";
  264. this.button8.UseVisualStyleBackColor = true;
  265. this.button8.Click += new System.EventHandler(this.button8_Click);
  266. //
  267. // trackBar2
  268. //
  269. this.trackBar2.Cursor = System.Windows.Forms.Cursors.Hand;
  270. this.trackBar2.Location = new System.Drawing.Point(7, 181);
  271. this.trackBar2.Maximum = 255;
  272. this.trackBar2.Name = "trackBar2";
  273. this.trackBar2.Size = new System.Drawing.Size(185, 45);
  274. this.trackBar2.TabIndex = 3;
  275. this.trackBar2.TickStyle = System.Windows.Forms.TickStyle.None;
  276. this.trackBar2.Value = 125;
  277. this.trackBar2.Scroll += new System.EventHandler(this.trackBar2_Scroll);
  278. //
  279. // pictureBox2
  280. //
  281. this.pictureBox2.Location = new System.Drawing.Point(7, 47);
  282. this.pictureBox2.Name = "pictureBox2";
  283. this.pictureBox2.Size = new System.Drawing.Size(219, 127);
  284. this.pictureBox2.TabIndex = 2;
  285. this.pictureBox2.TabStop = false;
  286. //
  287. // button6
  288. //
  289. this.button6.Location = new System.Drawing.Point(125, 20);
  290. this.button6.Name = "button6";
  291. this.button6.Size = new System.Drawing.Size(67, 21);
  292. this.button6.TabIndex = 1;
  293. this.button6.Text = "图库";
  294. this.button6.UseVisualStyleBackColor = true;
  295. this.button6.Click += new System.EventHandler(this.button6_Click);
  296. //
  297. // button5
  298. //
  299. this.button5.Location = new System.Drawing.Point(52, 20);
  300. this.button5.Name = "button5";
  301. this.button5.Size = new System.Drawing.Size(67, 21);
  302. this.button5.TabIndex = 0;
  303. this.button5.Text = "硬盘";
  304. this.button5.UseVisualStyleBackColor = true;
  305. this.button5.Click += new System.EventHandler(this.button5_Click);
  306. //
  307. // ImageComparison
  308. //
  309. this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
  310. this.ClientSize = new System.Drawing.Size(646, 556);
  311. this.Controls.Add(this.groupBox4);
  312. this.Controls.Add(this.groupBox3);
  313. this.Controls.Add(this.groupBox2);
  314. this.Controls.Add(this.groupBox1);
  315. this.MinimizeBox = false;
  316. this.Name = "ImageComparison";
  317. this.Text = "图片对比";
  318. this.Resize += new System.EventHandler(this.ImageComparison_Resize);
  319. this.Controls.SetChildIndex(this.groupBox1, 0);
  320. this.Controls.SetChildIndex(this.groupBox2, 0);
  321. this.Controls.SetChildIndex(this.groupBox3, 0);
  322. this.Controls.SetChildIndex(this.groupBox4, 0);
  323. this.groupBox1.ResumeLayout(false);
  324. this.groupBox2.ResumeLayout(false);
  325. ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
  326. this.groupBox3.ResumeLayout(false);
  327. this.groupBox3.PerformLayout();
  328. ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
  329. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  330. this.groupBox4.ResumeLayout(false);
  331. this.groupBox4.PerformLayout();
  332. ((System.ComponentModel.ISupportInitialize)(this.trackBar2)).EndInit();
  333. ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
  334. this.ResumeLayout(false);
  335. }
  336. /// <summary>
  337. /// 关闭按钮
  338. /// </summary>
  339. /// <param name="sender"></param>
  340. /// <param name="e"></param>
  341. private void button1_Click(object sender, EventArgs e)
  342. {
  343. this.Close();
  344. }
  345. /// <summary>
  346. /// 第一张小图的图库选择按钮
  347. /// </summary>
  348. /// <param name="sender"></param>
  349. /// <param name="e"></param>
  350. private void button4_Click(object sender, EventArgs e)
  351. {
  352. //辅助判断点击的是第一个picturebox对应的按钮
  353. pictureBoxNum = 1;
  354. ImagePickerDialog(sender);
  355. }
  356. /// <summary>
  357. /// 第二张小图的图库选择按钮
  358. /// </summary>
  359. /// <param name="sender"></param>
  360. /// <param name="e"></param>
  361. private void button6_Click(object sender, EventArgs e)
  362. {
  363. //辅助判断点击的是第二个picturebox对应的按钮
  364. pictureBoxNum = 2;
  365. ImagePickerDialog(sender);
  366. }
  367. /// <summary>
  368. /// 弹出图库选择窗口
  369. /// </summary>
  370. /// <param name="sender"></param>
  371. private void ImagePickerDialog(object sender)
  372. {
  373. if (imagePickerDialog == null)
  374. {
  375. imagePickerDialog = new InterImagePickerDialog(this.appWorkspace);
  376. imagePickerDialog.StartPosition = FormStartPosition.Manual;
  377. imagePickerDialog.Left = ((Button)sender).Left + 12 + this.Left + this.groupBox2.Width;
  378. switch (pictureBoxNum)
  379. {
  380. case 1:
  381. imagePickerDialog.Top = ((Button)sender).Bottom + 46 + this.Top + this.groupBox1.Height;
  382. break;
  383. case 2:
  384. imagePickerDialog.Top = ((Button)sender).Bottom + 50 + this.Top + this.groupBox1.Height + this.groupBox3.Height;
  385. break;
  386. }
  387. imagePickerDialog.ValueChanged += new System.EventHandler(this.inputMat_ValueChanged);
  388. imagePickerDialog.Show();
  389. }
  390. else
  391. {
  392. if (!imagePickerDialog.IsDisposed)
  393. {
  394. imagePickerDialog.WindowState = FormWindowState.Normal;
  395. }
  396. else
  397. {
  398. imagePickerDialog = new InterImagePickerDialog(this.appWorkspace);
  399. imagePickerDialog.StartPosition = FormStartPosition.Manual;
  400. imagePickerDialog.Left = ((Button)sender).Left + 12 + this.Left + this.groupBox2.Width;
  401. switch (pictureBoxNum)
  402. {
  403. case 1:
  404. imagePickerDialog.Top = ((Button)sender).Bottom + 46 + this.Top + this.groupBox1.Height;
  405. break;
  406. case 2:
  407. imagePickerDialog.Top = ((Button)sender).Bottom + 50 + this.Top + this.groupBox1.Height + this.groupBox3.Height;
  408. break;
  409. }
  410. imagePickerDialog.ValueChanged += new System.EventHandler(this.inputMat_ValueChanged);
  411. imagePickerDialog.Show();
  412. }
  413. }
  414. }
  415. /// <summary>
  416. /// 从图库选择待对照图片
  417. /// </summary>
  418. /// <param name="sender"></param>
  419. /// <param name="e"></param>
  420. private void inputMat_ValueChanged(object sender, EventArgs e)
  421. {
  422. if (sender is DocumentWorkspace)
  423. {
  424. switch (pictureBoxNum)
  425. {
  426. case 1:
  427. this.bitmap1 = ((DocumentWorkspace)sender).CompositionSurface.CreateAliasedBitmap();
  428. if (bitmap1.Width > pictureBox1.Size.Width || bitmap1.Height > pictureBox1.Size.Height)
  429. {
  430. pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
  431. }
  432. else
  433. {
  434. pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
  435. }
  436. this.pictureBox1.Image = this.bitmap1;
  437. CombinePicture();
  438. break;
  439. case 2:
  440. this.bitmap2 = ((DocumentWorkspace)sender).CompositionSurface.CreateAliasedBitmap();
  441. if (bitmap2.Width > pictureBox2.Size.Width || bitmap2.Height > pictureBox2.Size.Height)
  442. {
  443. pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
  444. }
  445. else
  446. {
  447. pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
  448. }
  449. this.pictureBox2.Image = this.bitmap2;
  450. CombinePicture();
  451. break;
  452. }
  453. }
  454. }
  455. /// <summary>
  456. /// 第一张小图的硬盘选择图片按钮
  457. /// </summary>
  458. /// <param name="sender"></param>
  459. /// <param name="e"></param>
  460. private void button3_Click(object sender, EventArgs e)
  461. {
  462. pictureBoxNum = 1;
  463. Bitmap newBit1 = ChooseImageFromHardDisk();//暂时接收一下,如果为空则不替换原有的原图对象
  464. if (newBit1 != null)
  465. {
  466. this.bitmap1 = newBit1;
  467. if (bitmap1.Width > pictureBox1.Size.Width || bitmap1.Height > pictureBox1.Size.Height)
  468. {
  469. pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
  470. }
  471. else
  472. {
  473. pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
  474. }
  475. this.pictureBox1.Image = this.bitmap1;
  476. CombinePicture();
  477. }
  478. }
  479. /// <summary>
  480. /// 第二张小图的硬盘选择图片按钮
  481. /// </summary>
  482. /// <param name="sender"></param>
  483. /// <param name="e"></param>
  484. private void button5_Click(object sender, EventArgs e)
  485. {
  486. pictureBoxNum = 2;
  487. Bitmap newBit2 = ChooseImageFromHardDisk();//暂时接收一下,如果为空则不替换原有的原图对象
  488. if (newBit2 != null)
  489. {
  490. this.bitmap2 = newBit2;
  491. if (bitmap2.Width > pictureBox2.Size.Width || bitmap2.Height > pictureBox2.Size.Height)
  492. {
  493. pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
  494. }
  495. else
  496. {
  497. pictureBox2.SizeMode = PictureBoxSizeMode.CenterImage;
  498. }
  499. this.pictureBox2.Image = this.bitmap2;
  500. CombinePicture();
  501. }
  502. }
  503. /// <summary>
  504. /// 从硬盘选择图片
  505. /// </summary>
  506. /// <returns></returns>
  507. private Bitmap ChooseImageFromHardDisk()
  508. {
  509. var openFileDialog = new OpenFileDialog { Filter = "*.jpg,*jpeg,*.bmp,*.ico,*.png,*.tif,*.wmf|*.jpg;*jpeg;*.bmp;*.ico;*.png;*.tif;*.wmf" };
  510. if (openFileDialog.ShowDialog() == DialogResult.OK)
  511. {
  512. Image image = Image.FromFile(openFileDialog.FileName);
  513. Bitmap newBit = new Bitmap(image);
  514. return newBit;
  515. }
  516. else
  517. {
  518. return null;
  519. }
  520. }
  521. /// <summary>
  522. /// 返回重新设置过透明度的图片
  523. /// </summary>
  524. /// <param name="image"></param>
  525. /// <param name="opcity"></param>
  526. /// <returns></returns>
  527. private Bitmap GetTransparencyImage(Image image, int opcity)
  528. {
  529. if (opcity < 0 || opcity > 255)
  530. return null;
  531. if (image == null)
  532. return null;
  533. float imgTransparency = float.Parse(opcity.ToString()) / 255;
  534. float[][] nArray ={ new float[] {1, 0, 0, 0, 0},
  535. new float[] {0, 1, 0, 0, 0},
  536. new float[] {0, 0, 1, 0, 0},
  537. new float[] {0, 0, 0, imgTransparency, 0},
  538. new float[] {0, 0, 0, 0, 1}};
  539. ColorMatrix matrix = new ColorMatrix(nArray);
  540. ImageAttributes attributes = new ImageAttributes();
  541. attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  542. Bitmap resultImage = new Bitmap(image.Width, image.Height);
  543. Graphics g = Graphics.FromImage(resultImage);
  544. g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
  545. return resultImage;
  546. }
  547. /// <summary>
  548. /// 第一张小图的透明度滚动条
  549. /// </summary>
  550. /// <param name="sender"></param>
  551. /// <param name="e"></param>
  552. private void trackBar1_Scroll(object sender, EventArgs e)
  553. {
  554. if (this.pictureBox1.Image != null)
  555. {
  556. CombinePicture();
  557. }
  558. }
  559. /// <summary>
  560. /// 第二张小图的透明度滚动条
  561. /// </summary>
  562. /// <param name="sender"></param>
  563. /// <param name="e"></param>
  564. private void trackBar2_Scroll(object sender, EventArgs e)
  565. {
  566. if (this.pictureBox2.Image != null)
  567. {
  568. CombinePicture();
  569. }
  570. }
  571. /// <summary>
  572. /// 合并两个小picturebox的图片
  573. /// </summary>
  574. private void CombinePicture()
  575. {
  576. if (this.pictureBox1.Image == null && this.pictureBox2.Image == null)
  577. return;
  578. if (this.pictureBox1.Image != null && this.pictureBox2.Image == null)
  579. {
  580. combineBitmap = GetTransparencyImage(this.pictureBox1.Image, this.trackBar1.Value);
  581. ordered = true;
  582. }
  583. if (this.pictureBox2.Image != null && this.pictureBox1.Image == null)
  584. {
  585. combineBitmap = GetTransparencyImage(this.pictureBox2.Image, this.trackBar2.Value);
  586. ordered = false;
  587. }
  588. if (this.pictureBox1.Image != null && this.pictureBox2.Image != null)
  589. {
  590. bitArray[0] = GetTransparencyImage(this.pictureBox1.Image, this.trackBar1.Value);
  591. bitArray[1] = GetTransparencyImage(this.pictureBox2.Image, this.trackBar2.Value);
  592. int[] widthArr = new int[] { bitArray[0].Width, bitArray[1].Width };
  593. int[] heightArr = new int[] { bitArray[0].Height, bitArray[1].Height };
  594. combineBitmap = new Bitmap(widthArr.Max(), heightArr.Max());
  595. Graphics g = Graphics.FromImage(combineBitmap);
  596. if (ordered)
  597. {
  598. g.DrawImage(bitArray[0], new PointF(0, 0));
  599. g.DrawImage(bitArray[1], new PointF(0, 0));
  600. }
  601. else
  602. {
  603. g.DrawImage(bitArray[1], new PointF(0, 0));
  604. g.DrawImage(bitArray[0], new PointF(0, 0));
  605. }
  606. }
  607. if (combineBitmap.Width > pictureBox3.Size.Width || combineBitmap.Height > pictureBox3.Size.Height)
  608. {
  609. pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
  610. }
  611. else
  612. {
  613. pictureBox3.SizeMode = PictureBoxSizeMode.CenterImage;
  614. }
  615. this.pictureBox3.Image = combineBitmap;
  616. }
  617. /// <summary>
  618. /// 第一张小图的层级向上按钮
  619. /// </summary>
  620. /// <param name="sender"></param>
  621. /// <param name="e"></param>
  622. private void button7_Click(object sender, EventArgs e)
  623. {
  624. if (this.pictureBox1.Image != null && this.pictureBox2.Image != null)
  625. {
  626. reverseOrder();
  627. CombinePicture();
  628. }
  629. }
  630. /// <summary>
  631. /// 第二个小图的层级向上按钮
  632. /// </summary>
  633. /// <param name="sender"></param>
  634. /// <param name="e"></param>
  635. private void button8_Click(object sender, EventArgs e)
  636. {
  637. if (this.pictureBox1.Image != null && this.pictureBox2.Image != null)
  638. {
  639. reverseOrder();
  640. CombinePicture();
  641. }
  642. }
  643. /// <summary>
  644. /// 反转图片的绘制顺序
  645. /// </summary>
  646. private void reverseOrder()
  647. {
  648. if (ordered)
  649. {
  650. ordered = false;
  651. }
  652. else
  653. {
  654. ordered = true;
  655. }
  656. }
  657. /// <summary>
  658. /// 创建图片
  659. /// </summary>
  660. /// <param name="sender"></param>
  661. /// <param name="e"></param>
  662. private void button2_Click(object sender, EventArgs e)
  663. {
  664. if (combineBitmap != null)
  665. {
  666. DocumentWorkspace dw = this.appWorkspace.AddNewDocumentWorkspace();
  667. dw.Document = Document.FromImageMat(Camera.Tools.ToMat(combineBitmap));
  668. dw.xmlSaveModel = Startup.instance.ruleDB;
  669. dw.InitRulerInfo();
  670. stepLength = "00" + Startup.instance.step_length;
  671. // TODO ZYH 这里的图像名称,需要走系统的配置
  672. dw.fileText = this.Text + "-" + stepLength;
  673. this.appWorkspace.ActiveDocumentWorkspace = dw;
  674. //this.appWorkspace.ActiveDocumentWorkspace.PanelBottom.documentStrip.ShowPictures = false;
  675. this.appWorkspace.ActiveDocumentWorkspace.Refresh();
  676. Startup.instance.step_length += 1;
  677. }
  678. }
  679. /// <summary>
  680. /// 窗体尺寸改变后重新判断合并图片的显示方式
  681. /// </summary>
  682. /// <param name="sender"></param>
  683. /// <param name="e"></param>
  684. private void ImageComparison_Resize(object sender, EventArgs e)
  685. {
  686. if (combineBitmap != null)
  687. {
  688. if (combineBitmap.Width > pictureBox3.Size.Width || combineBitmap.Height > pictureBox3.Size.Height)
  689. {
  690. pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
  691. }
  692. else
  693. {
  694. pictureBox3.SizeMode = PictureBoxSizeMode.CenterImage;
  695. }
  696. this.pictureBox3.Invalidate();
  697. }
  698. }
  699. }
  700. }