ImageDepthExtensionDialog.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. using PaintDotNet.Base.CommTool;
  2. using PaintDotNet.CustomControl;
  3. using PaintDotNet.ImageCollect.CameraEDOF;
  4. using PaintDotNet.Processing.DepthExtensionComponent;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace PaintDotNet.Processing
  16. {
  17. /// <summary>
  18. /// 景深扩展
  19. /// </summary>
  20. internal class ImageDepthExtensionDialog : PdnBaseForm
  21. {
  22. private Button button3;
  23. private GroupBox groupBox2;
  24. /// <summary>
  25. /// 辅助判断目前最新一个自定义控件的tag值
  26. /// </summary>
  27. private int toolsNum = 1;
  28. /// <summary>
  29. /// 图片选择窗口
  30. /// </summary>
  31. private InterImagePickerDialog imagePickerDialog;
  32. /// <summary>
  33. /// 辅助为合并的图片命名
  34. /// </summary>
  35. private int nameNum = 1;
  36. /// <summary>
  37. /// 是否重置picturebox中的矩形尺寸
  38. /// </summary>
  39. private bool isFirst = true;
  40. /// <summary>
  41. /// 是否为拖动picturebox中的矩形框
  42. /// </summary>
  43. private bool isMoved = false;
  44. /// <summary>
  45. /// 是否为修改picturebox中的矩形框大小
  46. /// </summary>
  47. private bool isResized = false;
  48. /// <summary>
  49. /// 辅助计算偏移量
  50. /// </summary>
  51. private Point lastPoint = new Point(0, 0);
  52. /// <summary>
  53. /// picturebox中图片对应矩形
  54. /// </summary>
  55. private Rectangle imageRect;
  56. /// <summary>
  57. /// picturebox中选择框对应矩形
  58. /// </summary>
  59. private Rectangle zoomRect;
  60. /// <summary>
  61. /// 合成后的图片
  62. /// </summary>
  63. private Bitmap depthBitmap;
  64. private Label label1;
  65. private Button button7;
  66. private TextBox textBox1;
  67. private PictureBox pictureBox1;
  68. private Panel panel1;
  69. private GroupBox groupBox3;
  70. private AppWorkspace appWorkspace;
  71. public ImageDepthExtensionDialog(AppWorkspace appWorkspace)
  72. {
  73. this.appWorkspace = appWorkspace;
  74. InitializeComponent();
  75. InitializeLanguageText();
  76. InitializeOtherInfo();
  77. AddDepthExtensionPicTools();
  78. }
  79. private void InitializeLanguageText()
  80. {
  81. //this.groupBox1.Text = PdnResources.GetString("Menu.operation.text");
  82. //this.button4.Text = PdnResources.GetString("Menu.return.text");
  83. this.button3.Text = PdnResources.GetString("Menu.ronghe.Text");
  84. //this.button2.Text = PdnResources.GetString("Menu.Secondstep.text");
  85. //this.button1.Text = PdnResources.GetString("Menu.firststep.text");
  86. this.groupBox2.Text = PdnResources.GetString("Menu.Selectimagefrom.text");
  87. //this.button6.Text = PdnResources.GetString("Menu.Currentsoftware.text");
  88. //this.button5.Text = PdnResources.GetString("Menu.Theharddiskimage.Text");
  89. this.groupBox3.Text = PdnResources.GetString("Menu.Preview.text");
  90. //this.groupBox4.Text = PdnResources.GetString("Menu.finish.text");
  91. this.button7.Text = PdnResources.GetString("Menu.File.Save.Text");
  92. this.label1.Text = PdnResources.GetString("Menu.Imagename.text") + ":";
  93. this.Text = PdnResources.GetString("Menu.Image.DepthExtension.Text");
  94. }
  95. /// <summary>
  96. /// 初始化基础控件
  97. /// </summary>
  98. private void InitializeComponent()
  99. {
  100. this.button3 = new System.Windows.Forms.Button();
  101. this.groupBox2 = new System.Windows.Forms.GroupBox();
  102. this.textBox1 = new System.Windows.Forms.TextBox();
  103. this.label1 = new System.Windows.Forms.Label();
  104. this.button7 = new System.Windows.Forms.Button();
  105. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  106. this.panel1 = new System.Windows.Forms.Panel();
  107. this.groupBox3 = new System.Windows.Forms.GroupBox();
  108. this.groupBox2.SuspendLayout();
  109. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  110. this.groupBox3.SuspendLayout();
  111. this.SuspendLayout();
  112. //
  113. // button3
  114. //
  115. this.button3.Location = new System.Drawing.Point(442, 20);
  116. this.button3.Name = "button3";
  117. this.button3.Size = new System.Drawing.Size(75, 23);
  118. this.button3.TabIndex = 2;
  119. this.button3.Text = "图像融合";
  120. this.button3.UseVisualStyleBackColor = true;
  121. this.button3.Click += new System.EventHandler(this.button3_Click);
  122. //
  123. // groupBox2
  124. //
  125. this.groupBox2.Controls.Add(this.textBox1);
  126. this.groupBox2.Controls.Add(this.button3);
  127. this.groupBox2.Controls.Add(this.label1);
  128. this.groupBox2.Controls.Add(this.button7);
  129. this.groupBox2.Location = new System.Drawing.Point(15, 9);
  130. this.groupBox2.Name = "groupBox2";
  131. this.groupBox2.Size = new System.Drawing.Size(623, 60);
  132. this.groupBox2.TabIndex = 1;
  133. this.groupBox2.TabStop = false;
  134. //
  135. // textBox1
  136. //
  137. this.textBox1.Location = new System.Drawing.Point(72, 22);
  138. this.textBox1.Name = "textBox1";
  139. this.textBox1.ReadOnly = true;
  140. this.textBox1.Size = new System.Drawing.Size(167, 21);
  141. this.textBox1.TabIndex = 2;
  142. //
  143. // label1
  144. //
  145. this.label1.AutoSize = true;
  146. this.label1.Location = new System.Drawing.Point(7, 25);
  147. this.label1.Name = "label1";
  148. this.label1.Size = new System.Drawing.Size(65, 12);
  149. this.label1.TabIndex = 0;
  150. this.label1.Text = "图像名称:";
  151. //
  152. // button7
  153. //
  154. this.button7.Enabled = false;
  155. this.button7.Location = new System.Drawing.Point(537, 20);
  156. this.button7.Name = "button7";
  157. this.button7.Size = new System.Drawing.Size(75, 23);
  158. this.button7.TabIndex = 1;
  159. this.button7.Text = "保存";
  160. this.button7.UseVisualStyleBackColor = true;
  161. this.button7.Click += new System.EventHandler(this.button7_Click);
  162. //
  163. // pictureBox1
  164. //
  165. this.pictureBox1.Location = new System.Drawing.Point(6, 14);
  166. this.pictureBox1.Name = "pictureBox1";
  167. this.pictureBox1.Size = new System.Drawing.Size(611, 491);
  168. this.pictureBox1.TabIndex = 0;
  169. this.pictureBox1.TabStop = false;
  170. this.pictureBox1.Visible = false;
  171. //
  172. // panel1
  173. //
  174. this.panel1.AutoScroll = true;
  175. this.panel1.BackColor = System.Drawing.Color.Transparent;
  176. this.panel1.Location = new System.Drawing.Point(6, 14);
  177. this.panel1.Name = "panel1";
  178. this.panel1.Size = new System.Drawing.Size(611, 491);
  179. this.panel1.TabIndex = 0;
  180. //
  181. // groupBox3
  182. //
  183. this.groupBox3.Controls.Add(this.panel1);
  184. this.groupBox3.Controls.Add(this.pictureBox1);
  185. this.groupBox3.Location = new System.Drawing.Point(15, 85);
  186. this.groupBox3.Name = "groupBox3";
  187. this.groupBox3.Size = new System.Drawing.Size(623, 511);
  188. this.groupBox3.TabIndex = 2;
  189. this.groupBox3.TabStop = false;
  190. this.groupBox3.Text = "预览";
  191. //
  192. // ImageDepthExtensionDialog
  193. //
  194. this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
  195. this.ClientSize = new System.Drawing.Size(653, 612);
  196. this.Controls.Add(this.groupBox3);
  197. this.Controls.Add(this.groupBox2);
  198. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  199. this.MaximizeBox = false;
  200. this.MinimizeBox = false;
  201. this.Name = "ImageDepthExtensionDialog";
  202. this.Text = "景深扩展";
  203. this.Controls.SetChildIndex(this.groupBox2, 0);
  204. this.Controls.SetChildIndex(this.groupBox3, 0);
  205. this.groupBox2.ResumeLayout(false);
  206. this.groupBox2.PerformLayout();
  207. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  208. this.groupBox3.ResumeLayout(false);
  209. this.ResumeLayout(false);
  210. }
  211. /// <summary>
  212. /// 初始化其他
  213. /// </summary>
  214. private void InitializeOtherInfo()
  215. {
  216. //第一次进入页面的"第一步"按钮样式
  217. //this.button1.BackColor = Color.Gray;
  218. //this.button1.ForeColor = Color.White;
  219. //this.button1.FlatStyle = FlatStyle.Flat;
  220. //this.button1.FlatAppearance.BorderSize = 0;
  221. this.textBox1.Text = GetDepthImageName();
  222. this.pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
  223. this.pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
  224. this.pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
  225. }
  226. /// <summary>
  227. /// 添加一个图片的自定义控件
  228. /// </summary>
  229. private void AddDepthExtensionPicTools()
  230. {
  231. //判断panel上存在几个图片自定义控件
  232. if (this.panel1.Controls.Count > 0)
  233. {
  234. int toolControls = 0;
  235. foreach (Control C in this.panel1.Controls)
  236. {
  237. if (C is DepthExtensionPicTool)
  238. toolControls++;
  239. }
  240. if (toolControls > 0)
  241. {
  242. int rowNum = toolControls / 5;//判断行数
  243. int coloumNum = toolControls % 5;//判断列数
  244. Point panelScroll = this.panel1.AutoScrollPosition;
  245. DepthExtensionPicTool depthExtensionPicTool = new DepthExtensionPicTool();
  246. depthExtensionPicTool.Tag = toolsNum;
  247. depthExtensionPicTool.Size = new Size(this.panel1.Width / 5 - 6, this.panel1.Width / 5 - 6);
  248. depthExtensionPicTool.Location = new Point(0 + ((this.panel1.Width / 5 - 4) * coloumNum), 4 + ((this.panel1.Width / 5) * rowNum) + panelScroll.Y);
  249. depthExtensionPicTool.panel1.Cursor = Cursors.Hand;
  250. depthExtensionPicTool.panel1.Paint += new PaintEventHandler(panelDel_Paint);
  251. depthExtensionPicTool.panel1.Click += new EventHandler(panelDel_Click);
  252. depthExtensionPicTool.panel2.Cursor = Cursors.Hand;
  253. depthExtensionPicTool.panel2.Paint += new PaintEventHandler(panelAdd_Paint);
  254. depthExtensionPicTool.panel2.Click += new EventHandler(panelAdd_Click);
  255. this.panel1.Controls.Add(depthExtensionPicTool);
  256. }
  257. else
  258. {
  259. //第一个自定义控件,理论上无法走进这个判断,可删除
  260. DepthExtensionPicTool depthExtensionPicTool = new DepthExtensionPicTool();
  261. depthExtensionPicTool.Tag = toolsNum;
  262. depthExtensionPicTool.Size = new Size(this.panel1.Width / 5 - 6, this.panel1.Width / 5 - 6);
  263. depthExtensionPicTool.Location = new Point(0, 4);
  264. depthExtensionPicTool.panel1.Cursor = Cursors.Hand;
  265. depthExtensionPicTool.panel1.Paint += new PaintEventHandler(panelDel_Paint);
  266. depthExtensionPicTool.panel1.Click += new EventHandler(panelDel_Click);
  267. depthExtensionPicTool.panel2.Cursor = Cursors.Hand;
  268. depthExtensionPicTool.panel2.Paint += new PaintEventHandler(panelAdd_Paint);
  269. depthExtensionPicTool.panel2.Click += new EventHandler(panelAdd_Click);
  270. this.panel1.Controls.Add(depthExtensionPicTool);
  271. }
  272. }
  273. else
  274. {
  275. //实例化第一个自定义图片控件,坐标固定
  276. DepthExtensionPicTool depthExtensionPicTool = new DepthExtensionPicTool();
  277. depthExtensionPicTool.Tag = toolsNum;
  278. depthExtensionPicTool.Size = new Size(this.panel1.Width / 5 - 6, this.panel1.Width / 5 - 6);
  279. depthExtensionPicTool.Location = new Point(0, 4);
  280. depthExtensionPicTool.panel1.Cursor = Cursors.Hand;
  281. depthExtensionPicTool.panel1.Paint += new PaintEventHandler(panelDel_Paint);
  282. depthExtensionPicTool.panel1.Click += new EventHandler(panelDel_Click);
  283. depthExtensionPicTool.panel2.Cursor = Cursors.Hand;
  284. depthExtensionPicTool.panel2.Paint += new PaintEventHandler(panelAdd_Paint);
  285. depthExtensionPicTool.panel2.Click += new EventHandler(panelAdd_Click);
  286. this.panel1.Controls.Add(depthExtensionPicTool);
  287. }
  288. picturePath = new PicturePath();
  289. picturePath.Size = new Size(this.panel1.Width / 5 - 6, this.panel1.Width / 5 - 6);
  290. picturePath.button5.Click += new EventHandler(this.button5_Click);
  291. picturePath.button6.Click += new EventHandler(this.button6_Click);
  292. toolsNum++;
  293. }
  294. PicturePath picturePath;
  295. /// <summary>
  296. /// 绘制删除图标
  297. /// </summary>
  298. /// <param name="sender"></param>
  299. /// <param name="e"></param>
  300. private void panelDel_Paint(object sender, PaintEventArgs e)
  301. {
  302. Image deleteImg = PdnResources.GetImageResource("Images.ImageStrip.CloseButton.Hot.png").Reference;//从项目中随便找了一个红色的删除图片
  303. if (deleteImg != null)
  304. {
  305. Graphics g = e.Graphics;
  306. g.DrawImage(deleteImg, 0, 0);
  307. }
  308. }
  309. /// <summary>
  310. /// 绘制添加功能的十字线
  311. /// </summary>
  312. /// <param name="sender"></param>
  313. /// <param name="e"></param>
  314. private void panelAdd_Paint(object sender, PaintEventArgs e)
  315. {
  316. Panel panel = (Panel)sender;
  317. Size panelSize = panel.Size;
  318. Graphics g = e.Graphics;
  319. Pen linePen = new Pen(Color.Black);
  320. linePen.Width = 3;
  321. g.DrawLine(linePen, new Point(0, panelSize.Height / 2), new Point(panelSize.Width, panelSize.Height / 2));
  322. g.DrawLine(linePen, new Point(panelSize.Width / 2, 0), new Point(panelSize.Width / 2, panelSize.Height));
  323. linePen.Dispose();
  324. }
  325. /// <summary>
  326. /// 点击添加标志
  327. /// </summary>
  328. /// <param name="sender"></param>
  329. /// <param name="e"></param>
  330. private void panelAdd_Click(object sender, EventArgs e)
  331. {
  332. this.panel1.Controls.Add(picturePath);
  333. this.panel1.Controls[this.panel1.Controls.Count - 1].Location = this.panel1.Controls[this.panel1.Controls.Count - 2].Location;
  334. this.panel1.Controls[this.panel1.Controls.Count - 2].Visible = false;
  335. //this.panel1.Controls.Remove(picturePath);
  336. //List<Bitmap> listBitmap = ChooseImageFromHardDisk();
  337. //if (listBitmap != null && listBitmap.Count > 0)
  338. //{
  339. // for (int i = 0; i < listBitmap.Count; i++)
  340. // {
  341. // foreach (Control C in this.panel1.Controls)
  342. // {
  343. // if (C is DepthExtensionPicTool && Convert.ToInt32(C.Tag) == (toolsNum - 1))//取得当前最新的一个图片自定义控件
  344. // {
  345. // DepthExtensionPicTool latestDepthTool = (DepthExtensionPicTool)C;
  346. // if (listBitmap[i].Width > latestDepthTool.pictureBox1.Size.Width || listBitmap[i].Height > latestDepthTool.pictureBox1.Size.Height)
  347. // {
  348. // latestDepthTool.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
  349. // }
  350. // else
  351. // {
  352. // latestDepthTool.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
  353. // }
  354. // latestDepthTool.pictureBox1.Image = listBitmap[i];
  355. // latestDepthTool.panel2.Visible = false;
  356. // latestDepthTool.panel1.Visible = true;
  357. // AddDepthExtensionPicTools();
  358. // }
  359. // }
  360. // }
  361. //}
  362. //Bitmap bitmap = ChooseImageFromHardDisk();
  363. //if (bitmap != null)
  364. //{
  365. // Panel panel = (Panel)sender;
  366. // DepthExtensionPicTool parentTool = (DepthExtensionPicTool)panel.Parent;
  367. // if (bitmap.Width > parentTool.pictureBox1.Size.Width || bitmap.Height > parentTool.pictureBox1.Size.Height)
  368. // {
  369. // parentTool.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
  370. // }
  371. // else
  372. // {
  373. // parentTool.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
  374. // }
  375. // parentTool.pictureBox1.Image = bitmap;
  376. // parentTool.panel2.Visible = false;
  377. // parentTool.panel1.Visible = true;
  378. // AddDepthExtensionPicTools();
  379. //}
  380. }
  381. /// <summary>
  382. /// 点击删除图标
  383. /// </summary>
  384. /// <param name="sender"></param>
  385. /// <param name="e"></param>
  386. private void panelDel_Click(object sender, EventArgs e)
  387. {
  388. Panel panel = (Panel)sender;
  389. DepthExtensionPicTool parentTool = (DepthExtensionPicTool)panel.Parent;
  390. //判断当前存在几个自定义图片控件
  391. int toolControls = 0;
  392. foreach (Control C in this.panel1.Controls)
  393. {
  394. if (C is DepthExtensionPicTool)
  395. toolControls++;
  396. }
  397. if (toolControls == 1)
  398. {
  399. //只有一个该控件时不删除当前操作控件
  400. if (parentTool.pictureBox1.Image != null)
  401. {
  402. parentTool.pictureBox1.Image = null;
  403. parentTool.panel2.Visible = true;
  404. parentTool.panel1.Visible = false;
  405. }
  406. }
  407. else
  408. {
  409. this.panel1.Controls.Remove(parentTool);
  410. ResizeAllDepthTools();
  411. }
  412. }
  413. /// <summary>
  414. /// 重置所有图片控件的位置
  415. /// </summary>
  416. private void ResizeAllDepthTools()
  417. {
  418. if (this.panel1.Controls.Count > 1)
  419. {
  420. this.panel1.Controls[this.panel1.Controls.Count - 2].Visible = true;
  421. foreach (Control c in this.panel1.Controls)
  422. {
  423. if (c is PicturePath)
  424. {
  425. this.panel1.Controls.Remove(c);
  426. }
  427. }
  428. }
  429. List<DepthExtensionPicTool> listTools = new List<DepthExtensionPicTool>();
  430. foreach (Control C in this.panel1.Controls)
  431. {
  432. if (C is DepthExtensionPicTool)
  433. listTools.Add((DepthExtensionPicTool)C);
  434. }
  435. Point panelScroll = this.panel1.AutoScrollPosition;//panel的滚轮位置
  436. for (int i = 0; i < listTools.Count; i++)
  437. {
  438. int rowNum = i / 5;//判断行数
  439. int coloumNum = i % 5;//判断列数
  440. listTools[i].Location = new Point(0 + ((this.panel1.Width / 5 - 4) * coloumNum), 4 + ((this.panel1.Width / 5) * rowNum) + panelScroll.Y);
  441. }
  442. }
  443. /// <summary>
  444. /// 图库选择按钮
  445. /// </summary>
  446. /// <param name="sender"></param>
  447. /// <param name="e"></param>
  448. private void button6_Click(object sender, EventArgs e)
  449. {
  450. this.panel1.Controls[this.panel1.Controls.Count - 2].Visible = true;
  451. //this.panel1.Controls.Remove(this.panel1.Controls.Remove(this.panel1.Controls[this.panel1.Controls.Count - 1]));
  452. foreach (Control c in this.panel1.Controls)
  453. {
  454. if (c is PicturePath)
  455. {
  456. this.panel1.Controls.Remove(c);
  457. }
  458. }
  459. if (imagePickerDialog == null)
  460. {
  461. imagePickerDialog = new InterImagePickerDialog(this.appWorkspace);
  462. imagePickerDialog.StartPosition = FormStartPosition.Manual;
  463. imagePickerDialog.Left = ((Button)sender).Left + 12 + this.Left;
  464. imagePickerDialog.Top = ((Button)sender).Bottom + 36 + this.Top;
  465. imagePickerDialog.ValueChanged += new System.EventHandler(this.inputMat_ValueChanged);
  466. imagePickerDialog.Show();
  467. }
  468. else
  469. {
  470. if (!imagePickerDialog.IsDisposed)
  471. {
  472. imagePickerDialog.WindowState = FormWindowState.Normal;
  473. }
  474. else
  475. {
  476. imagePickerDialog = new InterImagePickerDialog(this.appWorkspace);
  477. imagePickerDialog.StartPosition = FormStartPosition.Manual;
  478. imagePickerDialog.Left = ((Button)sender).Left + 12 + this.Left;
  479. imagePickerDialog.Top = ((Button)sender).Bottom + 36 + this.Top;
  480. imagePickerDialog.ValueChanged += new System.EventHandler(this.inputMat_ValueChanged);
  481. imagePickerDialog.Show();
  482. }
  483. }
  484. }
  485. /// <summary>
  486. /// 从图库选择待对照图片
  487. /// </summary>
  488. /// <param name="sender"></param>
  489. /// <param name="e"></param>
  490. private void inputMat_ValueChanged(object sender, EventArgs e)
  491. {
  492. if (sender is DocumentWorkspace)
  493. {
  494. Bitmap bitmap = ((DocumentWorkspace)sender).CompositionSurface.CreateAliasedBitmap();
  495. foreach(Control C in this.panel1.Controls)
  496. {
  497. if(C.Tag != null && C is DepthExtensionPicTool)
  498. {
  499. int nowNum = Convert.ToInt32(C.Tag);
  500. if (nowNum == (toolsNum - 1))//取得当前最新的一个图片自定义控件
  501. {
  502. DepthExtensionPicTool depthExtensionPicTool = (DepthExtensionPicTool)C;
  503. if (bitmap.Width > depthExtensionPicTool.pictureBox1.Size.Width || bitmap.Height > depthExtensionPicTool.pictureBox1.Size.Height)
  504. {
  505. depthExtensionPicTool.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
  506. }
  507. else
  508. {
  509. depthExtensionPicTool.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
  510. }
  511. depthExtensionPicTool.pictureBox1.Image = bitmap;
  512. depthExtensionPicTool.panel2.Visible = false;
  513. depthExtensionPicTool.panel1.Visible = true;
  514. AddDepthExtensionPicTools();
  515. return;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. /// <summary>
  522. /// 从硬盘选择单张图片
  523. /// </summary>
  524. /// <returns></returns>
  525. private List<Bitmap> ChooseImageFromHardDisk()
  526. {
  527. List<Bitmap> bitmaps = new List<Bitmap>();
  528. var openFileDialog = new OpenFileDialog { Filter = "*.jpg,*jpeg,*.bmp,*.ico,*.png,*.tif,*.wmf|*.jpg;*jpeg;*.bmp;*.ico;*.png;*.tif;*.wmf" };
  529. openFileDialog.Multiselect = true;
  530. string[] picArr;
  531. if (openFileDialog.ShowDialog() == DialogResult.OK)
  532. {
  533. picArr = openFileDialog.FileNames;
  534. foreach (string item in picArr)
  535. {
  536. Image image = Image.FromFile(item);
  537. Bitmap newBit = new Bitmap(image);
  538. bitmaps.Add(newBit);
  539. }
  540. return bitmaps;
  541. }
  542. else
  543. {
  544. return null;
  545. }
  546. //var openFileDialog = new OpenFileDialog { Filter = "*.jpg,*jpeg,*.bmp,*.ico,*.png,*.tif,*.wmf|*.jpg;*jpeg;*.bmp;*.ico;*.png;*.tif;*.wmf" };
  547. //if (openFileDialog.ShowDialog() == DialogResult.OK)
  548. //{
  549. // Image image = Image.FromFile(openFileDialog.FileName);
  550. // Bitmap newBit = new Bitmap(image);
  551. // return newBit;
  552. //}
  553. //else
  554. //{
  555. // return null;
  556. //}
  557. }
  558. /// <summary>
  559. /// 从硬盘选择多张图片
  560. /// </summary>
  561. /// <returns></returns>
  562. private List<Bitmap> ChooseImagesFromHardDisk()
  563. {
  564. var openFileDialog = new OpenFileDialog { Filter = "*.jpg,*jpeg,*.bmp,*.ico,*.png,*.tif,*.wmf|*.jpg;*jpeg;*.bmp;*.ico;*.png;*.tif;*.wmf" };
  565. openFileDialog.Multiselect = true;
  566. if (openFileDialog.ShowDialog() == DialogResult.OK)
  567. {
  568. string[] imageNames = openFileDialog.FileNames;
  569. List<Bitmap> listBitmap = new List<Bitmap>();
  570. foreach(string imgName in imageNames)
  571. {
  572. Image image = Image.FromFile(imgName);
  573. listBitmap.Add(new Bitmap(image));
  574. }
  575. return listBitmap;
  576. }
  577. else
  578. {
  579. return null;
  580. }
  581. }
  582. /// <summary>
  583. /// 硬盘选择图片按钮
  584. /// </summary>
  585. /// <param name="sender"></param>
  586. /// <param name="e"></param>
  587. private void button5_Click(object sender, EventArgs e)
  588. {
  589. List<Bitmap> listBitmap = ChooseImagesFromHardDisk();
  590. this.panel1.Controls[this.panel1.Controls.Count - 2].Visible = true;
  591. foreach (Control c in this.panel1.Controls)
  592. {
  593. if (c is PicturePath)
  594. {
  595. this.panel1.Controls.Remove(c);
  596. }
  597. }
  598. if (listBitmap != null && listBitmap.Count > 0)
  599. {
  600. for (int i = 0; i < listBitmap.Count; i++)
  601. {
  602. foreach (Control C in this.panel1.Controls)
  603. {
  604. if (C is DepthExtensionPicTool && Convert.ToInt32(C.Tag) == (toolsNum - 1))//取得当前最新的一个图片自定义控件
  605. {
  606. DepthExtensionPicTool latestDepthTool = (DepthExtensionPicTool)C;
  607. if (listBitmap[i].Width > latestDepthTool.pictureBox1.Size.Width || listBitmap[i].Height > latestDepthTool.pictureBox1.Size.Height)
  608. {
  609. latestDepthTool.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
  610. }
  611. else
  612. {
  613. latestDepthTool.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
  614. }
  615. latestDepthTool.pictureBox1.Image = listBitmap[i];
  616. latestDepthTool.panel2.Visible = false;
  617. latestDepthTool.panel1.Visible = true;
  618. AddDepthExtensionPicTools();
  619. }
  620. }
  621. }
  622. }
  623. }
  624. /// <summary>
  625. /// 图像融合
  626. /// </summary>
  627. /// <param name="sender"></param>
  628. /// <param name="e"></param>
  629. private void button3_Click(object sender, EventArgs e)
  630. {
  631. if (PdnResources.GetString("Menu.ronghe.Text").Equals(this.button3.Text))
  632. {
  633. if (this.pictureBox1.Visible == true /*|| this.groupBox4.Visible == true*/)
  634. return;
  635. List<Bitmap> listBitmaps = new List<Bitmap>();
  636. foreach (Control C in this.panel1.Controls)
  637. {
  638. if (C is DepthExtensionPicTool)
  639. {
  640. DepthExtensionPicTool depthExtensionPicTool = (DepthExtensionPicTool)C;
  641. if (depthExtensionPicTool.pictureBox1.Image != null)
  642. {
  643. listBitmaps.Add(new Bitmap(depthExtensionPicTool.pictureBox1.Image));
  644. }
  645. }
  646. }
  647. //没有上传或选择图片不进行下一步
  648. if (listBitmaps == null || listBitmaps.Count == 0)
  649. {
  650. MessageBox.Show(PdnResources.GetString("Menu.Pleaseopenthepicture.Text"));
  651. return;
  652. }
  653. //this.panel1.Visible = false;
  654. //this.groupBox2.Visible = false;
  655. //this.pictureBox1.Visible = true;
  656. //this.groupBox4.Visible = true;
  657. //this.button1.BackColor = Color.FromArgb(255, 240, 240, 240);
  658. //this.button1.ForeColor = Color.FromArgb(255, 0, 0, 0);
  659. //this.button1.FlatStyle = FlatStyle.Standard;
  660. //this.button1.FlatAppearance.BorderSize = 1;
  661. //this.button2.BackColor = Color.Gray;
  662. //this.button2.ForeColor = Color.White;
  663. //this.button2.FlatStyle = FlatStyle.Flat;
  664. //this.button2.FlatAppearance.BorderSize = 0;
  665. String errorMsg = "";
  666. try
  667. {
  668. depthBitmap = GetDepthExtensionImage(listBitmaps, ref errorMsg);
  669. }
  670. catch (Exception)
  671. {
  672. MessageBox.Show(PdnResources.GetString("Menu.Depthoffieldexpansionfailed.Text"));
  673. depthBitmap = null;
  674. }
  675. finally
  676. {
  677. if (errorMsg != null)
  678. depthBitmap = null;
  679. }
  680. if (depthBitmap != null)
  681. {
  682. this.panel1.Visible = false;
  683. //this.groupBox2.Visible = false;
  684. this.pictureBox1.Visible = true;
  685. //this.groupBox4.Visible = true;
  686. //this.button1.BackColor = Color.FromArgb(255, 240, 240, 240);
  687. //this.button1.ForeColor = Color.FromArgb(255, 0, 0, 0);
  688. //this.button1.FlatStyle = FlatStyle.Standard;
  689. //this.button1.FlatAppearance.BorderSize = 1;
  690. //this.button2.BackColor = Color.Gray;
  691. //this.button2.ForeColor = Color.White;
  692. //this.button2.FlatStyle = FlatStyle.Flat;
  693. //this.button2.FlatAppearance.BorderSize = 0;
  694. if (depthBitmap.Width > this.pictureBox1.Size.Width || depthBitmap.Height > this.pictureBox1.Size.Height)
  695. {
  696. this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
  697. }
  698. else
  699. {
  700. this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
  701. }
  702. this.pictureBox1.Image = depthBitmap;
  703. lastPoint = new Point(0, 0);
  704. isFirst = true;
  705. }
  706. if (depthBitmap != null)
  707. {
  708. this.button3.Text = PdnResources.GetString("Menu.Previous.text");
  709. this.button7.Enabled = true;
  710. }
  711. }
  712. else if (PdnResources.GetString("Menu.Previous.text").Equals(this.button3.Text))
  713. {
  714. if (this.panel1.Visible == true /*|| this.groupBox2.Visible == true*/)
  715. return;
  716. this.button3.Text = PdnResources.GetString("Menu.ronghe.Text");
  717. this.button7.Enabled = false;
  718. this.pictureBox1.Visible = false;
  719. //this.groupBox4.Visible = false;
  720. this.panel1.Visible = true;
  721. //this.groupBox2.Visible = true;
  722. }
  723. //lastPoint = new Point(0, 0);
  724. //isFirst = true;
  725. }
  726. /// <summary>
  727. /// 返回按钮
  728. /// </summary>
  729. /// <param name="sender"></param>
  730. /// <param name="e"></param>
  731. private void button4_Click(object sender, EventArgs e)
  732. {
  733. if (this.panel1.Visible == true || this.groupBox2.Visible == true)
  734. return;
  735. this.pictureBox1.Visible = false;
  736. //this.groupBox4.Visible = false;
  737. this.panel1.Visible = true;
  738. this.groupBox2.Visible = true;
  739. //this.button2.BackColor = Color.FromArgb(255, 240, 240, 240);
  740. //this.button2.ForeColor = Color.FromArgb(255, 0, 0, 0);
  741. //this.button2.FlatStyle = FlatStyle.Standard;
  742. //this.button2.FlatAppearance.BorderSize = 1;
  743. //this.button1.BackColor = Color.Gray;
  744. //this.button1.ForeColor = Color.White;
  745. //this.button1.FlatStyle = FlatStyle.Flat;
  746. //this.button1.FlatAppearance.BorderSize = 0;
  747. }
  748. /// <summary>
  749. /// 第一步按钮
  750. /// </summary>
  751. /// <param name="sender"></param>
  752. /// <param name="e"></param>
  753. private void button1_Click(object sender, EventArgs e)
  754. {
  755. //this.button4.PerformClick();
  756. }
  757. /// <summary>
  758. /// 第二步按钮
  759. /// </summary>
  760. /// <param name="sender"></param>
  761. /// <param name="e"></param>
  762. private void button2_Click(object sender, EventArgs e)
  763. {
  764. this.button3.PerformClick();
  765. }
  766. /// <summary>
  767. /// 生成景深扩展后的图片名
  768. /// </summary>
  769. /// <returns></returns>
  770. private string GetDepthImageName()
  771. {
  772. int numLength = nameNum.ToString().Length;
  773. string joinName = "";
  774. switch (numLength)
  775. {
  776. case 1:
  777. joinName = "00" + nameNum;
  778. break;
  779. case 2:
  780. joinName = "0" + nameNum;
  781. break;
  782. default:
  783. joinName = nameNum.ToString();
  784. break;
  785. }
  786. return PdnResources.GetString("Menu.Image.Text")+"-"+ PdnResources.GetString("Menu.Image.DepthExtension.Text")+ "-" + joinName;
  787. }
  788. /// <summary>
  789. /// 保存按钮
  790. /// </summary>
  791. /// <param name="sender"></param>
  792. /// <param name="e"></param>
  793. private void button7_Click(object sender, EventArgs e)
  794. {
  795. if (this.pictureBox1.Image != null)
  796. {
  797. double widthRatio = imageRect.Width / (depthBitmap.Width * 1d);//缩略图与原图的宽度比
  798. double heightRatio = imageRect.Height / (depthBitmap.Height * 1d);//缩略图与原图的高度比
  799. int xc = (this.pictureBox1.Width - this.imageRect.Width) / 2;//缩略图横坐标与控件原点的偏移量
  800. int yc = (this.pictureBox1.Height - this.imageRect.Height) / 2;//缩略图纵坐标与控件原点的偏移量
  801. int cutX = Convert.ToInt32((zoomRect.X - xc) / widthRatio);
  802. int cutY = Convert.ToInt32((zoomRect.Y - yc) / heightRatio);
  803. int cutWidth = Convert.ToInt32(zoomRect.Width / widthRatio);
  804. int cutHeight = Convert.ToInt32(zoomRect.Height / heightRatio);
  805. Bitmap cutBitmap = DrawRulerHelper.KiCut(new Bitmap(this.pictureBox1.Image), cutX, cutY, cutWidth, cutHeight);
  806. if (cutBitmap != null)
  807. {
  808. DocumentWorkspace dw = appWorkspace.AddNewDocumentWorkspace();
  809. Document document = Document.FromImageMat(Camera.Tools.ToMat(cutBitmap));
  810. dw.Document = document;
  811. dw.xmlSaveModel = Startup.instance.ruleDB;
  812. dw.InitRulerInfo();
  813. dw.fileText = this.textBox1.Text;
  814. appWorkspace.ActiveDocumentWorkspace = dw;
  815. }
  816. nameNum++;
  817. this.textBox1.Text = GetDepthImageName();
  818. }
  819. }
  820. /// <summary>
  821. /// 绘制picturebox中的矩形框
  822. /// </summary>
  823. /// <param name="sender"></param>
  824. /// <param name="e"></param>
  825. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  826. {
  827. if (this.pictureBox1.Image != null)
  828. {
  829. Pen rectPen = new Pen(Color.LightGreen);
  830. rectPen.Width = 3;
  831. if (isFirst)
  832. {
  833. //获取缩略图对应矩形
  834. PropertyInfo _ImageRectanglePropert = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
  835. imageRect = (Rectangle)_ImageRectanglePropert.GetValue(this.pictureBox1, null);
  836. zoomRect = new Rectangle(imageRect.X, imageRect.Y, imageRect.Width, imageRect.Height);
  837. }
  838. e.Graphics.DrawRectangle(rectPen, zoomRect.X, zoomRect.Y, zoomRect.Width - 1, zoomRect.Height - 1);
  839. e.Graphics.FillRectangle(new SolidBrush(Color.Black), new Rectangle(zoomRect.Right - 5, zoomRect.Bottom - 5, 7, 7));
  840. rectPen.Dispose();
  841. }
  842. }
  843. /// <summary>
  844. /// 矩形框的选中
  845. /// </summary>
  846. /// <param name="sender"></param>
  847. /// <param name="e"></param>
  848. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  849. {
  850. lastPoint.X = e.X;
  851. lastPoint.Y = e.Y;
  852. //是否平移
  853. if (zoomRect.Contains(e.Location) && !new Rectangle(zoomRect.Right - 5, zoomRect.Bottom - 5, 7, 7).Contains(e.Location))
  854. isMoved = true;
  855. else
  856. isMoved = false;
  857. //是否调整大小
  858. if (new Rectangle(zoomRect.Right - 5, zoomRect.Bottom - 5, 7, 7).Contains(e.Location))
  859. isResized = true;
  860. else
  861. isResized = false;
  862. }
  863. /// <summary>
  864. /// 矩形框的拖动和改变大小
  865. /// </summary>
  866. /// <param name="sender"></param>
  867. /// <param name="e"></param>
  868. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  869. {
  870. //修改鼠标形状
  871. this.pictureBox1.Cursor = Cursors.Default;
  872. if (zoomRect != null)
  873. {
  874. if (zoomRect.Contains(e.Location) && !new Rectangle(zoomRect.Right - 5, zoomRect.Bottom - 5, 7, 7).Contains(e.Location))
  875. {
  876. this.pictureBox1.Cursor = Cursors.SizeAll;
  877. }
  878. if (new Rectangle(zoomRect.Right - 5, zoomRect.Bottom - 5, 7, 7).Contains(e.Location))
  879. {
  880. this.pictureBox1.Cursor = Cursors.SizeNWSE;
  881. }
  882. }
  883. if (e.Button != MouseButtons.Left)
  884. return;
  885. //拖拽
  886. if (zoomRect != null && isMoved)
  887. {
  888. isFirst = false;
  889. Point p = e.Location;
  890. int dx = p.X - lastPoint.X;
  891. int dy = p.Y - lastPoint.Y;
  892. lastPoint.X = p.X;
  893. lastPoint.Y = p.Y;
  894. zoomRect.X += dx;
  895. zoomRect.Y += dy;
  896. //控制矩形框不超过缩略图尺寸
  897. int xc = (this.pictureBox1.Width - this.imageRect.Width) / 2;
  898. int yc = (this.pictureBox1.Height - this.imageRect.Height) / 2;
  899. if (zoomRect.X < xc) zoomRect.X = xc;
  900. if (zoomRect.Y < yc) zoomRect.Y = yc;
  901. if (zoomRect.X + zoomRect.Width > imageRect.Width + xc) zoomRect.X = imageRect.Width - zoomRect.Width + xc;
  902. if (zoomRect.Y + zoomRect.Height > imageRect.Height + yc) zoomRect.Y = imageRect.Height - zoomRect.Height + yc;
  903. this.pictureBox1.Refresh();
  904. }
  905. //调整大小
  906. if (zoomRect != null && isResized)
  907. {
  908. isFirst = false;
  909. Point p = e.Location;
  910. //不允许超出矩形框的原点
  911. if (p.X < zoomRect.X + 5)
  912. {
  913. p.X = zoomRect.X + 5;
  914. }
  915. if (p.Y < zoomRect.Y + 5)
  916. {
  917. p.Y = zoomRect.Y + 5;
  918. }
  919. //修改矩形框尺寸
  920. int top = zoomRect.Top;
  921. int left = zoomRect.Left;
  922. int right = zoomRect.Right;
  923. int bottom = zoomRect.Bottom;
  924. right = p.X;
  925. bottom = p.Y;
  926. zoomRect = new Rectangle(left, top, right - left, bottom - top);
  927. //控制矩形框不超过缩略图尺寸
  928. int xc = (this.pictureBox1.Width - this.imageRect.Width) / 2;
  929. int yc = (this.pictureBox1.Height - this.imageRect.Height) / 2;
  930. if (zoomRect.X < xc) zoomRect.X = xc;
  931. if (zoomRect.Y < yc) zoomRect.Y = yc;
  932. if (zoomRect.X + zoomRect.Width > imageRect.Width + xc) zoomRect.Width = imageRect.Width + xc - zoomRect.X;
  933. if (zoomRect.Y + zoomRect.Height > imageRect.Height + yc) zoomRect.Height = imageRect.Height + yc - zoomRect.Y;
  934. this.pictureBox1.Refresh();
  935. }
  936. }
  937. /// <summary>
  938. /// 实现景深扩展,返回图片
  939. /// </summary>
  940. private Bitmap GetDepthExtensionImage(List<Bitmap> listBitmaps, ref String errorMsg)
  941. {
  942. OpenCvSharp.Mat[] mats = new OpenCvSharp.Mat[listBitmaps.Count];
  943. for (int i = 0; i < listBitmaps.Count; i++)
  944. {
  945. mats[i] = PaintDotNet.Camera.Tools.ToMat(listBitmaps[i]);
  946. }
  947. if (mats != null && mats.Count() > 0)
  948. {
  949. Bitmap depthBitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(Merge.GetMergeMat(mats, ref errorMsg));
  950. return depthBitmap;
  951. }
  952. else
  953. return null;
  954. }
  955. }
  956. }