FrameCameraControl.cs 36 KB

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