FrameCameraControl.cs 34 KB

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