FrameControl.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using PaintDotNet.Base.SettingModel;
  11. using System.Collections;
  12. using PaintDotNet.ImageCollect;
  13. using System.IO;
  14. using System.Runtime.Serialization.Formatters.Binary;
  15. using System.Threading;
  16. using System.Drawing.Imaging;
  17. using PaintDotNet.Camera;
  18. namespace PaintDotNet.Preview2
  19. {
  20. /// <summary>
  21. /// 相机设置,边框界面
  22. /// </summary>
  23. public class FrameControl : UserControl
  24. {
  25. #region 控件
  26. private ComboBox cameraComboBox;
  27. private GroupBox groupBox3;
  28. public PictureBox pictureBox1;
  29. private Label label1;
  30. private TextBox txbX;
  31. private TextBox txbY;
  32. private Label label2;
  33. private TextBox txbH;
  34. private TextBox txbW;
  35. private Label label4;
  36. private Button btnFull;
  37. private Button btnCenter;
  38. private Label label3;
  39. private GroupBox groupBox2;
  40. private ComboBox cmbResolution;
  41. private Button btnLast;
  42. private Button btnSet;
  43. private System.Windows.Forms.Timer timer1;
  44. private IContainer components;
  45. private TableLayoutPanel tableLayoutPanel1;
  46. private ComboBox comboBox1;
  47. private Button btnRefresh;
  48. private void InitializeComponent()
  49. {
  50. this.components = new System.ComponentModel.Container();
  51. this.cameraComboBox = new System.Windows.Forms.ComboBox();
  52. this.groupBox3 = new System.Windows.Forms.GroupBox();
  53. this.txbH = new System.Windows.Forms.TextBox();
  54. this.comboBox1 = new System.Windows.Forms.ComboBox();
  55. this.btnSet = new System.Windows.Forms.Button();
  56. this.btnLast = new System.Windows.Forms.Button();
  57. this.label3 = new System.Windows.Forms.Label();
  58. this.btnFull = new System.Windows.Forms.Button();
  59. this.btnCenter = new System.Windows.Forms.Button();
  60. this.txbW = new System.Windows.Forms.TextBox();
  61. this.label4 = new System.Windows.Forms.Label();
  62. this.txbY = new System.Windows.Forms.TextBox();
  63. this.label2 = new System.Windows.Forms.Label();
  64. this.txbX = new System.Windows.Forms.TextBox();
  65. this.label1 = new System.Windows.Forms.Label();
  66. this.btnRefresh = new System.Windows.Forms.Button();
  67. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  68. this.groupBox2 = new System.Windows.Forms.GroupBox();
  69. this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
  70. this.cmbResolution = new System.Windows.Forms.ComboBox();
  71. this.timer1 = new System.Windows.Forms.Timer(this.components);
  72. this.groupBox3.SuspendLayout();
  73. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
  74. this.groupBox2.SuspendLayout();
  75. this.tableLayoutPanel1.SuspendLayout();
  76. this.SuspendLayout();
  77. //
  78. // cameraComboBox
  79. //
  80. this.cameraComboBox.Dock = System.Windows.Forms.DockStyle.Fill;
  81. this.cameraComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  82. this.cameraComboBox.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  83. this.cameraComboBox.FormattingEnabled = true;
  84. this.cameraComboBox.Location = new System.Drawing.Point(3, 27);
  85. this.cameraComboBox.Name = "cameraComboBox";
  86. this.cameraComboBox.Size = new System.Drawing.Size(263, 20);
  87. this.cameraComboBox.TabIndex = 2;
  88. this.cameraComboBox.SelectedIndexChanged += new System.EventHandler(this.cameraComboBox_SelectedIndexChanged);
  89. //
  90. // groupBox3
  91. //
  92. this.groupBox3.Controls.Add(this.txbH);
  93. this.groupBox3.Controls.Add(this.comboBox1);
  94. this.groupBox3.Controls.Add(this.btnSet);
  95. this.groupBox3.Controls.Add(this.btnLast);
  96. this.groupBox3.Controls.Add(this.label3);
  97. this.groupBox3.Controls.Add(this.btnFull);
  98. this.groupBox3.Controls.Add(this.btnCenter);
  99. this.groupBox3.Controls.Add(this.txbW);
  100. this.groupBox3.Controls.Add(this.label4);
  101. this.groupBox3.Controls.Add(this.txbY);
  102. this.groupBox3.Controls.Add(this.label2);
  103. this.groupBox3.Controls.Add(this.txbX);
  104. this.groupBox3.Controls.Add(this.label1);
  105. this.groupBox3.Controls.Add(this.btnRefresh);
  106. this.groupBox3.Controls.Add(this.pictureBox1);
  107. this.groupBox3.Dock = System.Windows.Forms.DockStyle.Bottom;
  108. this.groupBox3.Location = new System.Drawing.Point(0, 92);
  109. this.groupBox3.Name = "groupBox3";
  110. this.groupBox3.Padding = new System.Windows.Forms.Padding(4, 6, 4, 4);
  111. this.groupBox3.Size = new System.Drawing.Size(277, 347);
  112. this.groupBox3.TabIndex = 2;
  113. this.groupBox3.TabStop = false;
  114. this.groupBox3.Text = "设置";
  115. //
  116. // txbH
  117. //
  118. this.txbH.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  119. this.txbH.Location = new System.Drawing.Point(150, 281);
  120. this.txbH.MaxLength = 5;
  121. this.txbH.Name = "txbH";
  122. this.txbH.Size = new System.Drawing.Size(56, 21);
  123. this.txbH.TabIndex = 9;
  124. this.txbH.Text = "100";
  125. this.txbH.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.zuobiaoXTextBox_KeyPress);
  126. //
  127. // comboBox1
  128. //
  129. this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  130. this.comboBox1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  131. this.comboBox1.FormattingEnabled = true;
  132. this.comboBox1.Items.AddRange(new object[] {
  133. "px",
  134. "μm"});
  135. this.comboBox1.Location = new System.Drawing.Point(216, 248);
  136. this.comboBox1.Name = "comboBox1";
  137. this.comboBox1.Size = new System.Drawing.Size(47, 20);
  138. this.comboBox1.TabIndex = 15;
  139. //
  140. // btnSet
  141. //
  142. this.btnSet.Location = new System.Drawing.Point(203, 313);
  143. this.btnSet.Name = "btnSet";
  144. this.btnSet.Size = new System.Drawing.Size(63, 23);
  145. this.btnSet.TabIndex = 14;
  146. this.btnSet.Text = "刷新";
  147. this.btnSet.UseVisualStyleBackColor = true;
  148. this.btnSet.Click += new System.EventHandler(this.btnSet_Click);
  149. //
  150. // btnLast
  151. //
  152. this.btnLast.Location = new System.Drawing.Point(73, 313);
  153. this.btnLast.Name = "btnLast";
  154. this.btnLast.Size = new System.Drawing.Size(63, 23);
  155. this.btnLast.TabIndex = 13;
  156. this.btnLast.Text = "上次";
  157. this.btnLast.UseVisualStyleBackColor = true;
  158. this.btnLast.Click += new System.EventHandler(this.btnLast_Click);
  159. //
  160. // label3
  161. //
  162. this.label3.AutoSize = true;
  163. this.label3.Location = new System.Drawing.Point(134, 289);
  164. this.label3.Name = "label3";
  165. this.label3.Size = new System.Drawing.Size(17, 12);
  166. this.label3.TabIndex = 12;
  167. this.label3.Text = "×";
  168. //
  169. // btnFull
  170. //
  171. this.btnFull.Location = new System.Drawing.Point(138, 313);
  172. this.btnFull.Name = "btnFull";
  173. this.btnFull.Size = new System.Drawing.Size(63, 23);
  174. this.btnFull.TabIndex = 11;
  175. this.btnFull.Text = "全幅";
  176. this.btnFull.UseVisualStyleBackColor = true;
  177. this.btnFull.Click += new System.EventHandler(this.btnFull_Click);
  178. //
  179. // btnCenter
  180. //
  181. this.btnCenter.Location = new System.Drawing.Point(8, 313);
  182. this.btnCenter.Name = "btnCenter";
  183. this.btnCenter.Size = new System.Drawing.Size(63, 23);
  184. this.btnCenter.TabIndex = 10;
  185. this.btnCenter.Text = "中心";
  186. this.btnCenter.UseVisualStyleBackColor = true;
  187. this.btnCenter.Click += new System.EventHandler(this.btnCenter_Click);
  188. //
  189. // txbW
  190. //
  191. this.txbW.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  192. this.txbW.Location = new System.Drawing.Point(76, 281);
  193. this.txbW.MaxLength = 5;
  194. this.txbW.Name = "txbW";
  195. this.txbW.Size = new System.Drawing.Size(56, 21);
  196. this.txbW.TabIndex = 7;
  197. this.txbW.Text = "100";
  198. this.txbW.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.zuobiaoXTextBox_KeyPress);
  199. //
  200. // label4
  201. //
  202. this.label4.AutoSize = true;
  203. this.label4.Location = new System.Drawing.Point(7, 284);
  204. this.label4.Name = "label4";
  205. this.label4.Size = new System.Drawing.Size(65, 12);
  206. this.label4.TabIndex = 6;
  207. this.label4.Text = "图像尺寸:";
  208. //
  209. // txbY
  210. //
  211. this.txbY.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  212. this.txbY.Location = new System.Drawing.Point(149, 247);
  213. this.txbY.MaxLength = 5;
  214. this.txbY.Name = "txbY";
  215. this.txbY.Size = new System.Drawing.Size(56, 21);
  216. this.txbY.TabIndex = 5;
  217. this.txbY.Text = "100";
  218. this.txbY.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.zuobiaoXTextBox_KeyPress);
  219. //
  220. // label2
  221. //
  222. this.label2.AutoSize = true;
  223. this.label2.Location = new System.Drawing.Point(134, 254);
  224. this.label2.Name = "label2";
  225. this.label2.Size = new System.Drawing.Size(17, 12);
  226. this.label2.TabIndex = 4;
  227. this.label2.Text = "×";
  228. //
  229. // txbX
  230. //
  231. this.txbX.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  232. this.txbX.Location = new System.Drawing.Point(76, 247);
  233. this.txbX.MaxLength = 5;
  234. this.txbX.Name = "txbX";
  235. this.txbX.Size = new System.Drawing.Size(56, 21);
  236. this.txbX.TabIndex = 3;
  237. this.txbX.Text = "100";
  238. this.txbX.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.zuobiaoXTextBox_KeyPress);
  239. //
  240. // label1
  241. //
  242. this.label1.AutoSize = true;
  243. this.label1.Location = new System.Drawing.Point(7, 253);
  244. this.label1.Name = "label1";
  245. this.label1.Size = new System.Drawing.Size(65, 12);
  246. this.label1.TabIndex = 2;
  247. this.label1.Text = "起始坐标:";
  248. //
  249. // btnRefresh
  250. //
  251. this.btnRefresh.Dock = System.Windows.Forms.DockStyle.Top;
  252. this.btnRefresh.Location = new System.Drawing.Point(4, 222);
  253. this.btnRefresh.Name = "btnRefresh";
  254. this.btnRefresh.Size = new System.Drawing.Size(269, 23);
  255. this.btnRefresh.TabIndex = 1;
  256. this.btnRefresh.Text = "刷新缩略图";
  257. this.btnRefresh.UseVisualStyleBackColor = true;
  258. this.btnRefresh.Click += new System.EventHandler(this.ThumbRefresh_Click);
  259. //
  260. // pictureBox1
  261. //
  262. this.pictureBox1.BackColor = System.Drawing.Color.DimGray;
  263. this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Top;
  264. this.pictureBox1.Location = new System.Drawing.Point(4, 20);
  265. this.pictureBox1.Name = "pictureBox1";
  266. this.pictureBox1.Size = new System.Drawing.Size(269, 202);
  267. this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
  268. this.pictureBox1.TabIndex = 0;
  269. this.pictureBox1.TabStop = false;
  270. this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.imageBox1_Paint);
  271. this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseDown);
  272. this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseMove);
  273. this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseUp);
  274. //
  275. // groupBox2
  276. //
  277. this.groupBox2.Controls.Add(this.tableLayoutPanel1);
  278. this.groupBox2.Dock = System.Windows.Forms.DockStyle.Top;
  279. this.groupBox2.Location = new System.Drawing.Point(0, 8);
  280. this.groupBox2.Margin = new System.Windows.Forms.Padding(2);
  281. this.groupBox2.Name = "groupBox2";
  282. this.groupBox2.Padding = new System.Windows.Forms.Padding(4, 6, 4, 4);
  283. this.groupBox2.Size = new System.Drawing.Size(277, 73);
  284. this.groupBox2.TabIndex = 3;
  285. this.groupBox2.TabStop = false;
  286. this.groupBox2.Text = "分辨率";
  287. //
  288. // tableLayoutPanel1
  289. //
  290. this.tableLayoutPanel1.ColumnCount = 1;
  291. this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
  292. this.tableLayoutPanel1.Controls.Add(this.cameraComboBox, 0, 1);
  293. this.tableLayoutPanel1.Controls.Add(this.cmbResolution, 0, 0);
  294. this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
  295. this.tableLayoutPanel1.Location = new System.Drawing.Point(4, 20);
  296. this.tableLayoutPanel1.Name = "tableLayoutPanel1";
  297. this.tableLayoutPanel1.RowCount = 2;
  298. this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
  299. this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
  300. this.tableLayoutPanel1.Size = new System.Drawing.Size(269, 49);
  301. this.tableLayoutPanel1.TabIndex = 3;
  302. //
  303. // cmbResolution
  304. //
  305. this.cmbResolution.Dock = System.Windows.Forms.DockStyle.Fill;
  306. this.cmbResolution.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
  307. this.cmbResolution.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  308. this.cmbResolution.FormattingEnabled = true;
  309. this.cmbResolution.Location = new System.Drawing.Point(3, 3);
  310. this.cmbResolution.Name = "cmbResolution";
  311. this.cmbResolution.Size = new System.Drawing.Size(263, 20);
  312. this.cmbResolution.TabIndex = 0;
  313. //
  314. // timer1
  315. //
  316. this.timer1.Enabled = true;
  317. this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
  318. //
  319. // FrameControl
  320. //
  321. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  322. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  323. this.Controls.Add(this.groupBox3);
  324. this.Controls.Add(this.groupBox2);
  325. this.Name = "FrameControl";
  326. this.Padding = new System.Windows.Forms.Padding(0, 8, 0, 0);
  327. this.Size = new System.Drawing.Size(277, 439);
  328. this.groupBox3.ResumeLayout(false);
  329. this.groupBox3.PerformLayout();
  330. ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
  331. this.groupBox2.ResumeLayout(false);
  332. this.tableLayoutPanel1.ResumeLayout(false);
  333. this.ResumeLayout(false);
  334. }
  335. #endregion
  336. /// <summary>
  337. /// 相机参数的Model
  338. /// </summary>
  339. private CameraParamModel.ParameterSets _settings
  340. {
  341. get
  342. {
  343. return CameraConfigs.Settings;
  344. }
  345. }
  346. /// <summary>
  347. /// 相机设置的dialog
  348. /// </summary>
  349. private Point m_startP; //画框的起始点
  350. private bool m_blnDraw = false;
  351. private Rectangle m_rect = new Rectangle();//初始设置一个拖拽的显示框
  352. SolidBrush m_brush = new SolidBrush(Color.Black);
  353. int m_handle = 0;
  354. Rectangle _lastRoiRect;
  355. Rectangle _nowRoiRect;
  356. private Size _resolution
  357. {
  358. get
  359. {
  360. var value = m_camera.Resolution;
  361. if ((_lastRotateId % 2) != 0)
  362. value = new Size(value.Height, value.Width);
  363. return value;
  364. }
  365. }// 分辨率
  366. private ICamera m_camera => CameraManager.CurrentCamera;
  367. string[] m_binArray = new string[] { "1x1 Bin", "2x2 Bin", "3x3 Bin", "4x4 Bin" };
  368. private int m_selectedBin;
  369. public FrameControl()
  370. {
  371. InitializeComponent();
  372. comboBox1.SelectedIndex = 0;
  373. comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
  374. }
  375. private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
  376. {
  377. UpdateRoiText();
  378. }
  379. protected override void Dispose(bool disposing = true)
  380. {
  381. timer1.Enabled = false;
  382. base.Dispose(disposing);
  383. }
  384. public void Initialize()
  385. {
  386. InitializeLanguageText();
  387. InitResolution();
  388. cmbResolution.SelectedIndexChanged += cmbResolution_SelectedIndexChanged;
  389. InitSettings();
  390. ThumbRefresh_Click(null, null);
  391. UpdateRoiText();
  392. }
  393. public void InitSettings()
  394. {
  395. try
  396. {
  397. cameraComboBox.SelectedIndex = _settings.BinningSumation - 1;
  398. cmbResolution.SelectedIndex = _settings.Resolution;
  399. _lastRotateId = _settings.Rotate;
  400. _nowRoiRect.X = _settings.previewRectX;
  401. _nowRoiRect.Y = _resolution.Height - _settings.previewRectY - _settings.previewRectH; ;
  402. _nowRoiRect.Width = _settings.previewRectW;
  403. _nowRoiRect.Height = _settings.previewRectH;
  404. }
  405. catch { }
  406. }
  407. public void UpdateSettings()
  408. {
  409. try
  410. {
  411. cameraComboBox.SelectedIndex = _settings.BinningSumation - 1;
  412. cmbResolution.SelectedIndex = _settings.Resolution;
  413. _nowRoiRect.X = _settings.previewRectX;
  414. _nowRoiRect.Y = _resolution.Height - _settings.previewRectY - _settings.previewRectH; ;
  415. _nowRoiRect.Width = _settings.previewRectW;
  416. _nowRoiRect.Height = _settings.previewRectH;
  417. }
  418. catch { }
  419. }
  420. private void InitializeLanguageText()
  421. {
  422. this.groupBox3.Text = PdnResources.GetString("Menu.Setting.Text");
  423. this.btnFull.Text = PdnResources.GetString("Menu.Fullframe.text");
  424. this.btnCenter.Text = PdnResources.GetString("Menu.center.text");
  425. this.label4.Text = PdnResources.GetString("Menu.Imagesize.text") + ":";
  426. this.label1.Text = PdnResources.GetString("Menu.Startingcoordinates.text") + ":";
  427. this.btnRefresh.Text = PdnResources.GetString("Menu.Refreshthumbnail.text");
  428. this.groupBox2.Text = PdnResources.GetString("Menu.Resolution.text");
  429. this.btnSet.Text = PdnResources.GetString("Menu.Refresh.text");
  430. this.btnLast.Text = PdnResources.GetString("Menu.Lasttime.text");
  431. }
  432. /// <summary>
  433. /// 设置下拉等数据源
  434. /// </summary>
  435. private void InitResolution()
  436. {
  437. this.cameraComboBox.Items.Clear();
  438. this.cameraComboBox.Items.AddRange(m_binArray);
  439. int index = _settings.BinningSumation - 1;
  440. if (index > m_binArray.Length || index < 0)
  441. {
  442. index = 0;
  443. }
  444. this.cameraComboBox.SelectedIndex = index;
  445. cmbResolution.Items.Clear();
  446. List<string> resolutions = this.m_camera.GetResolutionList();
  447. for (int i = 0; i < resolutions.Count; i++)
  448. {
  449. cmbResolution.Items.Add(resolutions[i]);
  450. }
  451. if (resolutions.Count > 0)
  452. {
  453. cmbResolution.SelectedIndex = _settings.Resolution;
  454. }
  455. }
  456. private void cameraComboBox_SelectedIndexChanged(object sender, EventArgs e)
  457. {
  458. m_selectedBin = cameraComboBox.SelectedIndex + 1;
  459. if (m_selectedBin == _settings.BinningSumation)
  460. {
  461. return;
  462. }
  463. _settings.BinningSumation = m_selectedBin;
  464. }
  465. private void cmbResolution_SelectedIndexChanged(object sender, EventArgs e)
  466. {
  467. _settings.Resolution = cmbResolution.SelectedIndex;
  468. int resolutionId = m_camera.ResolutionId;
  469. if (resolutionId != _settings.Resolution)
  470. {
  471. // 分辨率
  472. m_camera.ResolutionId = _settings.Resolution;
  473. ROIReset();
  474. }
  475. }
  476. #region Oprate Buttons
  477. //中心按钮被点击
  478. private void btnCenter_Click(object sender, EventArgs e)
  479. {
  480. if (btnCenter.Text.Equals(PdnResources.GetString("Menu.center.text")))
  481. {
  482. btnCenter.Text = PdnResources.GetString("Menu.Start.text");
  483. _nowRoiRect.X = (_resolution.Width - _nowRoiRect.Width) / 2;
  484. _nowRoiRect.Y = (_resolution.Height - _nowRoiRect.Height) / 2;
  485. }
  486. else
  487. {
  488. btnCenter.Text = PdnResources.GetString("Menu.center.text");
  489. _nowRoiRect.X = 0;
  490. _nowRoiRect.Y = 0;
  491. }
  492. SetROI();
  493. }
  494. //全幅
  495. private void btnFull_Click(object sender, EventArgs e)
  496. {
  497. _lastRoiRect = _nowRoiRect;
  498. _nowRoiRect = new Rectangle(0, 0, _resolution.Width, _resolution.Height);
  499. SetROI();
  500. }
  501. private void btnLast_Click(object sender, EventArgs e)
  502. {
  503. _nowRoiRect = _lastRoiRect;
  504. _lastRoiRect = Rectangle.Empty;
  505. SetROI();
  506. }
  507. /// <summary>
  508. /// 刷新缩略图
  509. /// </summary>
  510. /// <param name="sender"></param>
  511. /// <param name="e"></param>
  512. private void ThumbRefresh_Click(object sender, EventArgs e)
  513. {
  514. ShootAndShow();
  515. }
  516. private void ShootAndShow()
  517. {
  518. CameraManager.Shoot((bitmap) =>
  519. {
  520. if (bitmap == null)
  521. return;
  522. if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed)
  523. {
  524. ColorPalette palette = bitmap.Palette;
  525. for (int i = 0; i < 256; i++)
  526. {
  527. palette.Entries[i] = Color.FromArgb(i, i, i);
  528. }
  529. bitmap.Palette = palette;
  530. }
  531. this.pictureBox1.Image = bitmap;
  532. });
  533. }
  534. #endregion
  535. #region picture event
  536. private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
  537. {
  538. if (e.Button != MouseButtons.Left) return;
  539. m_handle = HitTest(e.Location);
  540. if (m_handle < 0)
  541. {
  542. if (m_rect.Contains(e.Location))//判断鼠标按下的坐标是否在红框中,确定是否拖动的红框
  543. {
  544. m_handle = 9;
  545. }
  546. }
  547. this.Cursor = GetHandleCursor(m_handle);
  548. m_startP = e.Location;
  549. m_blnDraw = true;
  550. }
  551. private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
  552. {
  553. if (m_handle > 0 && m_blnDraw)
  554. {
  555. MoveHandleTo(e.Location, m_handle);
  556. UpdateRoiText();
  557. }
  558. }
  559. private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
  560. {
  561. m_blnDraw = false; //结束绘制
  562. this.Cursor = Cursors.Default;
  563. SetROI();
  564. }
  565. private void imageBox1_Paint(object sender, PaintEventArgs e)
  566. {
  567. if (_resolution.Width != 0 && _resolution.Height != 0)
  568. {
  569. if (!m_blnDraw)
  570. {
  571. double _scarHor = pictureBox1.Width * 1.0 / _resolution.Width;
  572. double _scarVal = pictureBox1.Height * 1.0 / _resolution.Height;
  573. m_rect.X = (int)(_nowRoiRect.X * _scarHor);
  574. m_rect.Y = (int)(_nowRoiRect.Y * _scarVal);
  575. m_rect.Width = (int)(_nowRoiRect.Width * _scarHor);
  576. m_rect.Height = (int)(_nowRoiRect.Height * _scarVal);
  577. }
  578. if (m_rect.Width > 0 && m_rect.Height > 0)
  579. {
  580. e.Graphics.DrawRectangle(new Pen(Color.Red, 2), m_rect);//重新绘制颜色为红色
  581. for (int i = 1; i <= 8; i++)
  582. {
  583. e.Graphics.FillRectangle(m_brush, GetHandleRectangle(i));
  584. }
  585. }
  586. }
  587. }
  588. #endregion
  589. #region handle
  590. public Rectangle GetHandleRectangle(int handleNumber)
  591. {
  592. Point point = GetHandle(handleNumber);
  593. return new Rectangle(point.X - 4, point.Y - 4, 8, 8);
  594. }
  595. public int HitTest(Point point)
  596. {
  597. for (int i = 1; i <= 8; i++)
  598. {
  599. if (GetHandleRectangle(i).Contains(point))
  600. return i;
  601. }
  602. return -1;
  603. }
  604. public Point GetHandle(int handleNumber)
  605. {
  606. int x, y, xCenter, yCenter;
  607. xCenter = m_rect.X + m_rect.Width / 2;
  608. yCenter = m_rect.Y + m_rect.Height / 2;
  609. x = m_rect.X;
  610. y = m_rect.Y;
  611. switch (handleNumber)
  612. {
  613. case 1:
  614. x = m_rect.X;
  615. y = m_rect.Y;
  616. break;
  617. case 2:
  618. x = xCenter;
  619. y = m_rect.Y;
  620. break;
  621. case 3:
  622. x = m_rect.Right;
  623. y = m_rect.Y;
  624. break;
  625. case 4:
  626. x = m_rect.Right;
  627. y = yCenter;
  628. break;
  629. case 5:
  630. x = m_rect.Right;
  631. y = m_rect.Bottom;
  632. break;
  633. case 6:
  634. x = xCenter;
  635. y = m_rect.Bottom;
  636. break;
  637. case 7:
  638. x = m_rect.X;
  639. y = m_rect.Bottom;
  640. break;
  641. case 8:
  642. x = m_rect.X;
  643. y = yCenter;
  644. break;
  645. }
  646. return new Point(x, y);
  647. }
  648. public Cursor GetHandleCursor(int handleNumber)
  649. {
  650. switch (handleNumber)
  651. {
  652. case 1:
  653. return Cursors.SizeNWSE;
  654. case 2:
  655. return Cursors.SizeNS;
  656. case 3:
  657. return Cursors.SizeNESW;
  658. case 4:
  659. return Cursors.SizeWE;
  660. case 5:
  661. return Cursors.SizeNWSE;
  662. case 6:
  663. return Cursors.SizeNS;
  664. case 7:
  665. return Cursors.SizeNESW;
  666. case 8:
  667. return Cursors.SizeWE;
  668. default:
  669. return Cursors.Default;
  670. }
  671. }
  672. public void MoveHandleTo(Point point, int handleNumber)
  673. {
  674. point.X = point.X > pictureBox1.Width ? pictureBox1.Width : point.X;
  675. point.X = point.X < 0 ? 0 : point.X;
  676. point.Y = point.Y > pictureBox1.Height ? pictureBox1.Height : point.Y;
  677. point.Y = point.Y < 0 ? 0 : point.Y;
  678. int left = m_rect.Left;
  679. int top = m_rect.Top;
  680. int right = m_rect.Right;
  681. int bottom = m_rect.Bottom;
  682. switch (handleNumber)
  683. {
  684. case 1:
  685. left = point.X;
  686. top = point.Y;
  687. break;
  688. case 2:
  689. top = point.Y;
  690. break;
  691. case 3:
  692. right = point.X;
  693. top = point.Y;
  694. break;
  695. case 4:
  696. right = point.X;
  697. break;
  698. case 5:
  699. right = point.X;
  700. bottom = point.Y;
  701. break;
  702. case 6:
  703. bottom = point.Y;
  704. break;
  705. case 7:
  706. left = point.X;
  707. bottom = point.Y;
  708. break;
  709. case 8:
  710. left = point.X;
  711. break;
  712. case 9:
  713. left = m_rect.Left + (point.X - m_startP.X);
  714. top = m_rect.Top + (point.Y - m_startP.Y);
  715. //判断是否超过左上角
  716. if (left < 0)
  717. left = 0;
  718. if (top < 0)
  719. top = 0;
  720. //判断是否超过右下 角
  721. if (left > (pictureBox1.Width - m_rect.Width))
  722. left = pictureBox1.Width - m_rect.Width;
  723. if (top > (pictureBox1.Height - m_rect.Height))
  724. top = pictureBox1.Height - m_rect.Height;
  725. right = left + m_rect.Width;
  726. bottom = top + m_rect.Height;
  727. break;
  728. }
  729. m_rect.X = left;
  730. m_rect.Y = top;
  731. m_rect.Width = right - left;
  732. m_rect.Height = bottom - top;
  733. _nowRoiRect.X = m_rect.X * _resolution.Width / pictureBox1.Width;
  734. _nowRoiRect.Y = m_rect.Y * _resolution.Height / pictureBox1.Height;
  735. _nowRoiRect.Width = m_rect.Width * _resolution.Width / pictureBox1.Width;
  736. _nowRoiRect.Height = m_rect.Height * _resolution.Height / pictureBox1.Height;
  737. pictureBox1.Refresh();
  738. m_startP = point;
  739. }
  740. #endregion
  741. private void zuobiaoXTextBox_KeyPress(object sender, KeyPressEventArgs e)
  742. {
  743. if (e.KeyChar == (char)Keys.Enter)
  744. {
  745. var textBox = sender as Control;
  746. int val = Convert.ToInt32(textBox.Text);
  747. TextChange();
  748. }
  749. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
  750. {
  751. e.Handled = true;
  752. }
  753. }
  754. int TxtPixelX => GetPixelValue(txbX.Text);
  755. int TxtPixelY => GetPixelValue(txbY.Text);
  756. int TxtPixelW => GetPixelValue(txbW.Text);
  757. int TxtPixelH => GetPixelValue(txbH.Text);
  758. int GetPixelValue(string txt)
  759. {
  760. var value = Convert.ToDouble(txt);
  761. if (comboBox1.SelectedIndex == 1)
  762. {
  763. value = value / Startup.instance.rules[MeasurementUnit.Micron];
  764. }
  765. return (int)value;
  766. }
  767. private void TextChange()
  768. {
  769. _nowRoiRect = new Rectangle(TxtPixelX, TxtPixelY, TxtPixelW, TxtPixelH);
  770. SetROI();
  771. }
  772. private void SetROI()
  773. {
  774. var w = _nowRoiRect.Width;
  775. var h = _nowRoiRect.Height;
  776. var x = _nowRoiRect.X;
  777. var y = _resolution.Height - _nowRoiRect.Y - h;
  778. m_camera.SetROI(x, y, w, h);
  779. _settings.previewRectX = x;
  780. _settings.previewRectY = y;
  781. _settings.previewRectW = w;
  782. _settings.previewRectH = h;
  783. this.pictureBox1.Refresh();
  784. UpdateRoiText();
  785. }
  786. private void ROIReset()
  787. {
  788. _lastRoiRect = Rectangle.Empty;
  789. int x = 0, y = 0, w = _resolution.Width, h = _resolution.Height;
  790. _nowRoiRect = new Rectangle(x, y, w, h);
  791. ShootAndShow();
  792. SetROI();
  793. }
  794. private void timer1_Tick(object sender, EventArgs e)
  795. {
  796. if (!CameraManager.IsLive) return;
  797. btnLast.Enabled = _lastRoiRect != Rectangle.Empty;
  798. btnFull.Enabled = _nowRoiRect.Size != _resolution;
  799. btnFull.Enabled = _nowRoiRect.Size != _resolution;
  800. btnCenter.Enabled = _nowRoiRect.Size != _resolution;
  801. btnSet.Enabled = _nowRoiRect != GetTextRect() && !m_blnDraw;
  802. }
  803. private void UpdateRoiText()
  804. {
  805. double pxLength = Startup.instance.rules[MeasurementUnit.Micron];
  806. if (comboBox1.SelectedIndex == 1)
  807. {
  808. txbX.Text = (_nowRoiRect.X * pxLength).ToString("F2");
  809. txbY.Text = (_nowRoiRect.Y * pxLength).ToString("F2");
  810. txbW.Text = (_nowRoiRect.Width * pxLength).ToString("F2");
  811. txbH.Text = (_nowRoiRect.Height * pxLength).ToString("F2");
  812. }
  813. else if (comboBox1.SelectedIndex == 0)
  814. {
  815. txbX.Text = _nowRoiRect.X + "";
  816. txbY.Text = _nowRoiRect.Y + "";
  817. txbW.Text = _nowRoiRect.Width + "";
  818. txbH.Text = _nowRoiRect.Height + "";
  819. }
  820. }
  821. private Rectangle GetTextRect()
  822. {
  823. try
  824. {
  825. var x = Convert.ToInt32(this.txbX.Text);
  826. var y = Convert.ToInt32(this.txbY.Text);
  827. var w = Convert.ToInt32(this.txbW.Text);
  828. var h = Convert.ToInt32(this.txbH.Text);
  829. return new Rectangle(x, y, w, h);
  830. }
  831. catch { return new Rectangle(new Point(0, 0), _resolution); }
  832. }
  833. private void btnSet_Click(object sender, EventArgs e)
  834. {
  835. TextChange();
  836. }
  837. #region Transformation
  838. int _lastRotateId = 0;
  839. /// <summary>
  840. /// 当发生图像旋转后旋转ROI区域显示
  841. /// </summary>
  842. public void Transfor()
  843. {
  844. if (_lastRotateId == 0 && _settings.Rotate == 1
  845. || _lastRotateId == 2 && _settings.Rotate == 0)
  846. {
  847. R90();
  848. }
  849. if (_lastRotateId == 0 && _settings.Rotate == 2
  850. || _lastRotateId == 1 && _settings.Rotate == 0)
  851. {
  852. L90();
  853. }
  854. //if (_lastRotateId == 0 && _settings.Rotate == 2
  855. // || _lastRotateId == 1 && _settings.Rotate == 3)
  856. // R180();
  857. _lastRotateId = _settings.Rotate;
  858. UpdateRoiText();
  859. }
  860. void R90()
  861. {
  862. var newRect = new Rectangle();
  863. newRect.Width = _nowRoiRect.Height;
  864. newRect.Height = _nowRoiRect.Width;
  865. newRect.Y = _nowRoiRect.X;
  866. newRect.X = _resolution.Height - _nowRoiRect.Y - _nowRoiRect.Height;
  867. _nowRoiRect = newRect;
  868. }
  869. void L90()
  870. {
  871. var newRect = new Rectangle();
  872. newRect.Width = _nowRoiRect.Height;
  873. newRect.Height = _nowRoiRect.Width;
  874. newRect.Y = _resolution.Width - _nowRoiRect.X - _nowRoiRect.Width;
  875. newRect.X = _nowRoiRect.Y;
  876. _nowRoiRect = newRect;
  877. }
  878. void R180()
  879. {
  880. var newRect = new Rectangle();
  881. newRect.Width = _nowRoiRect.Width;
  882. newRect.Height = _nowRoiRect.Height;
  883. newRect.X = _resolution.Width - _nowRoiRect.X - _nowRoiRect.Width;
  884. newRect.Y = _resolution.Height - _nowRoiRect.Y - _nowRoiRect.Height;
  885. _nowRoiRect = newRect;
  886. }
  887. #endregion
  888. }
  889. }