ImageThreeDShowDialog.cs 39 KB

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