FrameCameraControl2.cs 35 KB

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