FrameCameraSet1Control.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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 TUCamera;
  12. using TUCAMAPI;
  13. using System.Runtime.InteropServices;
  14. namespace PaintDotNet.ImageCollect.CameraComponent
  15. {
  16. public partial class FrameCameraSet1Control : UserControl
  17. {
  18. private TUCamera.TUCamera m_camera;
  19. private CameraParamModel m_cameraParamModel;
  20. private bool m_use;
  21. private Point m_startP; //鼠标按下时坐标
  22. private bool m_blnDraw = false;
  23. private Rectangle m_rect;//初始设置一个拖拽的显示框
  24. private SolidBrush m_brush = new SolidBrush(Color.Black);
  25. private int m_handle = 0;
  26. private int m_targetX = 0; // 起始点X坐标 对应分辨率
  27. private int m_targetY = 0; // 起始点Y坐标 对应分辨率
  28. private int m_targetWidth = 320; //显示框的宽度 对应分辨率
  29. private int m_targetHeight = 200; //显示框的高度 对应分辨率
  30. private int m_tempX = 0;
  31. private int m_tempY = 0;
  32. private int m_tempW = 0;
  33. private int m_tempH = 0;
  34. private TUCameraIFrameProcess m_process = null;
  35. private Size m_resolution; // 分辨率
  36. private bool m_isWaiting;
  37. private void InitializeLanguageText()
  38. {
  39. this.groupBox3.Text = PdnResources.GetString("Menu.Setting.Text");
  40. this.quanfuButton.Text = PdnResources.GetString("Menu.Fullframe.text");
  41. this.zhongxinButton.Text = PdnResources.GetString("Menu.center.text");
  42. this.label4.Text = PdnResources.GetString("Menu.Imagesize.text") + ":";
  43. this.label1.Text = PdnResources.GetString("Menu.Startingcoordinates.text") + ":";
  44. this.shuaxinButton.Text = PdnResources.GetString("Menu.Refreshthumbnail.text");
  45. this.groupBox2.Text = PdnResources.GetString("Menu.Resolution.text");
  46. }
  47. public FrameCameraSet1Control(CameraParamModel model, bool use)
  48. {
  49. InitializeComponent();
  50. InitializeLanguageText();
  51. m_use = use;
  52. m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
  53. m_cameraParamModel = model;
  54. InitializeControlData();
  55. //InitializeData();
  56. }
  57. public void ReLoad(CameraParamModel model, bool use)
  58. {
  59. m_cameraParamModel = model;
  60. m_use = use;
  61. InitializeControlData();
  62. InitializeData();
  63. }
  64. private void InitializeControlData()
  65. {
  66. cmbResolution.Items.Clear();
  67. List<string> resolutions = this.m_camera.GetResolutionRange();
  68. for (int i = 0; i < resolutions.Count; i++)
  69. {
  70. cmbResolution.Items.Add(resolutions[i]);
  71. }
  72. if (resolutions.Count > 0)
  73. {
  74. cmbResolution.SelectedIndex = m_cameraParamModel.parame.Resolution;
  75. }
  76. //
  77. this.zuobiaoXTextBox.Text = m_cameraParamModel.parame.previewRectX.ToString();
  78. this.zuobiaoYTextBox.Text = m_cameraParamModel.parame.previewRectY.ToString();
  79. this.chicunWTextBox.Text = m_cameraParamModel.parame.previewRectW.ToString();
  80. this.chicunHTextBox.Text = m_cameraParamModel.parame.previewRectH.ToString();
  81. }
  82. /// <summary>
  83. /// 绑定样式数据
  84. /// </summary>
  85. private void InitializeData()
  86. {
  87. m_targetX = this.m_cameraParamModel.parame.previewRectX;
  88. m_targetY = this.m_cameraParamModel.parame.previewRectY;
  89. m_targetWidth = this.m_cameraParamModel.parame.previewRectW;
  90. m_targetHeight = this.m_cameraParamModel.parame.previewRectH;
  91. SetROI();
  92. m_rect = new Rectangle();
  93. drawingImg();
  94. if (m_resolution.Width > 0 && m_resolution.Height > 0)
  95. {
  96. this.pictureBox1.Height = (this.pictureBox1.Width * m_resolution.Height) / m_resolution.Width;
  97. m_rect.X = this.m_cameraParamModel.parame.previewRectX * this.pictureBox1.Width / m_resolution.Width;
  98. m_rect.Y = this.m_cameraParamModel.parame.previewRectY * this.pictureBox1.Height / m_resolution.Height;
  99. m_rect.Width = this.m_cameraParamModel.parame.previewRectW * this.pictureBox1.Width / m_resolution.Width;
  100. m_rect.Height = this.m_cameraParamModel.parame.previewRectH * this.pictureBox1.Height / m_resolution.Height;
  101. }
  102. else
  103. {
  104. m_resolution.Width = pictureBox1.Width;
  105. m_resolution.Height = pictureBox1.Height;
  106. m_rect.X = 0;
  107. m_rect.Y = 0;
  108. m_rect.Width = this.pictureBox1.Width;
  109. m_rect.Height = this.pictureBox1.Height;
  110. }
  111. }
  112. private void zhongxinButton_Click(object sender, EventArgs e)
  113. {
  114. if (zhongxinButton.Text.Equals(PdnResources.GetString("Menu.center.text")))
  115. {
  116. zhongxinButton.Text = PdnResources.GetString("Menu.Start.text");
  117. int X = pictureBox1.Width / 2 - m_rect.Width / 2;
  118. int Y = pictureBox1.Height / 2 - m_rect.Height / 2;
  119. m_rect.X = X;
  120. m_rect.Y = Y;
  121. m_targetX = m_rect.X * m_resolution.Width / this.pictureBox1.Width;
  122. m_targetY = m_rect.Y * m_resolution.Height / this.pictureBox1.Height;
  123. }
  124. else
  125. {
  126. zhongxinButton.Text = PdnResources.GetString("Menu.center.text");
  127. int X = 0;
  128. int Y = 0;
  129. m_rect.X = X;
  130. m_rect.Y = Y;
  131. m_targetX = m_rect.X * m_resolution.Width / this.pictureBox1.Width;
  132. m_targetY = m_rect.Y * m_resolution.Height / this.pictureBox1.Height;
  133. }
  134. SetROI();
  135. this.pictureBox1.Refresh();
  136. }
  137. private void quanfuButton_Click(object sender, EventArgs e)
  138. {
  139. if (this.quanfuButton.Text.Equals(PdnResources.GetString("Menu.Lasttime.text")))
  140. {
  141. this.quanfuButton.Text = PdnResources.GetString("Menu.Fullframe.text");
  142. if (m_resolution.Width>0 && m_resolution.Height>0)
  143. {
  144. m_rect.X = m_tempX * this.pictureBox1.Width / m_resolution.Width;
  145. m_rect.Y = m_tempY * this.pictureBox1.Height / m_resolution.Height;
  146. m_rect.Width = m_tempW * this.pictureBox1.Width / m_resolution.Width;
  147. m_rect.Height = m_tempH * this.pictureBox1.Height / m_resolution.Height;
  148. m_targetX = m_tempX;
  149. m_targetY = m_tempY;
  150. m_targetWidth = m_tempW;
  151. m_targetHeight = m_tempH;
  152. }
  153. }
  154. else
  155. {
  156. this.quanfuButton.Text = PdnResources.GetString("Menu.Lasttime.text");
  157. m_tempX = m_targetX;
  158. m_tempY = m_targetY;
  159. m_tempW = m_targetWidth;
  160. m_tempH = m_targetHeight;
  161. int X = 0;
  162. int Y = 0;
  163. m_rect.X = X;
  164. m_rect.Y = Y;
  165. m_rect.Width = this.pictureBox1.Width;
  166. m_rect.Height = this.pictureBox1.Height;
  167. m_targetX = 0;
  168. m_targetY = 0;
  169. m_targetWidth = m_resolution.Width;
  170. m_targetHeight = m_resolution.Height;
  171. }
  172. SetROI();
  173. this.pictureBox1.Refresh();
  174. }
  175. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  176. {
  177. m_handle = HitTest(e.Location);
  178. if (m_handle < 0)
  179. {
  180. if (m_rect.Contains(e.Location))//判断鼠标按下的坐标是否在红框中,确定是否拖动的红框
  181. {
  182. m_handle = 9;
  183. }
  184. }
  185. this.Cursor = GetHandleCursor(m_handle);
  186. m_startP = e.Location;
  187. Invalidate();
  188. m_blnDraw = true;
  189. }
  190. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  191. {
  192. if (m_handle > 0 && m_blnDraw)
  193. {
  194. if (e.Button != MouseButtons.Left)//判断是否按下左键
  195. return;
  196. MoveHandleTo(e.Location, m_handle);
  197. }
  198. }
  199. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  200. {
  201. m_blnDraw = false; //结束绘制
  202. this.Cursor = Cursors.Default;
  203. SetROI();
  204. this.pictureBox1.Refresh();
  205. }
  206. public Rectangle GetHandleRectangle(int handleNumber)
  207. {
  208. Point point = GetHandle(handleNumber);
  209. return new Rectangle(point.X - 4, point.Y - 4, 8, 8);
  210. }
  211. public int HitTest(Point point)
  212. {
  213. for (int i = 1; i <= 8; i++)
  214. {
  215. if (GetHandleRectangle(i).Contains(point))
  216. return i;
  217. }
  218. return -1;
  219. }
  220. public Point GetHandle(int handleNumber)
  221. {
  222. int x, y, xCenter, yCenter;
  223. xCenter = m_rect.X + m_rect.Width / 2;
  224. yCenter = m_rect.Y + m_rect.Height / 2;
  225. x = m_rect.X;
  226. y = m_rect.Y;
  227. switch (handleNumber)
  228. {
  229. case 1:
  230. x = m_rect.X;
  231. y = m_rect.Y;
  232. break;
  233. case 2:
  234. x = xCenter;
  235. y = m_rect.Y;
  236. break;
  237. case 3:
  238. x = m_rect.Right;
  239. y = m_rect.Y;
  240. break;
  241. case 4:
  242. x = m_rect.Right;
  243. y = yCenter;
  244. break;
  245. case 5:
  246. x = m_rect.Right;
  247. y = m_rect.Bottom;
  248. break;
  249. case 6:
  250. x = xCenter;
  251. y = m_rect.Bottom;
  252. break;
  253. case 7:
  254. x = m_rect.X;
  255. y = m_rect.Bottom;
  256. break;
  257. case 8:
  258. x = m_rect.X;
  259. y = yCenter;
  260. break;
  261. }
  262. return new Point(x, y);
  263. }
  264. public Cursor GetHandleCursor(int handleNumber)
  265. {
  266. switch (handleNumber)
  267. {
  268. case 1:
  269. return Cursors.SizeNWSE;
  270. case 2:
  271. return Cursors.SizeNS;
  272. case 3:
  273. return Cursors.SizeNESW;
  274. case 4:
  275. return Cursors.SizeWE;
  276. case 5:
  277. return Cursors.SizeNWSE;
  278. case 6:
  279. return Cursors.SizeNS;
  280. case 7:
  281. return Cursors.SizeNESW;
  282. case 8:
  283. return Cursors.SizeWE;
  284. default:
  285. return Cursors.Default;
  286. }
  287. }
  288. public void MoveHandleTo(Point point, int handleNumber)
  289. {
  290. if (point.X > pictureBox1.Width)
  291. {
  292. point.X = pictureBox1.Width;
  293. }
  294. if (point.X < 0)
  295. {
  296. point.X = 0;
  297. }
  298. if (point.Y < 0)
  299. {
  300. point.Y = 0;
  301. }
  302. if (point.Y > pictureBox1.Height)
  303. {
  304. point.Y = pictureBox1.Height;
  305. }
  306. int left = m_rect.Left;
  307. int top = m_rect.Top;
  308. int right = m_rect.Right;
  309. int bottom = m_rect.Bottom;
  310. switch (handleNumber)
  311. {
  312. case 1:
  313. left = point.X;
  314. top = point.Y;
  315. break;
  316. case 2:
  317. top = point.Y;
  318. break;
  319. case 3:
  320. right = point.X;
  321. top = point.Y;
  322. break;
  323. case 4:
  324. right = point.X;
  325. break;
  326. case 5:
  327. right = point.X;
  328. bottom = point.Y;
  329. break;
  330. case 6:
  331. bottom = point.Y;
  332. break;
  333. case 7:
  334. left = point.X;
  335. bottom = point.Y;
  336. break;
  337. case 8:
  338. left = point.X;
  339. break;
  340. case 9:
  341. left = m_rect.Left +(point.X - m_startP.X);
  342. top = m_rect.Top + (point.Y - m_startP.Y);
  343. //判断是否超过左上角
  344. if (left < 0)
  345. left = 0;
  346. if (top < 0)
  347. top = 0;
  348. //判断是否超过右下 角
  349. if (left > (pictureBox1.Width - m_rect.Width))
  350. left = pictureBox1.Width - m_rect.Width;
  351. if (top > (pictureBox1.Height - m_rect.Height))
  352. top = pictureBox1.Height - m_rect.Height;
  353. right = left + m_rect.Width;
  354. bottom = top + m_rect.Height;
  355. break;
  356. }
  357. m_rect.X = left;
  358. m_rect.Y = top;
  359. m_rect.Width = right - left;
  360. m_rect.Height = bottom - top;
  361. m_targetX = m_rect.X * m_resolution.Width / this.pictureBox1.Width;
  362. m_targetY = m_rect.Y * m_resolution.Height / this.pictureBox1.Height;
  363. m_targetWidth = m_rect.Width * m_resolution.Width / this.pictureBox1.Width;
  364. m_targetHeight = m_rect.Height * m_resolution.Height / this.pictureBox1.Height;
  365. pictureBox1.Refresh();
  366. m_startP = point;
  367. }
  368. private void drawingImg()
  369. {
  370. if (!m_camera.IsOpen())
  371. {
  372. return;
  373. }
  374. m_isWaiting = true;
  375. int resolution_width = pictureBox1.Width;
  376. int resolution_height = pictureBox1.Height;
  377. Console.WriteLine("点击刷新缩略图");
  378. if (m_camera.IsOpen()) {
  379. m_camera.m_drawAllHandler += new DrawAllHandler(CallbackDraw);
  380. m_camera.StartWaitForFrame();
  381. m_process = m_camera.StartDrawing(ref resolution_width, ref resolution_height);
  382. }
  383. m_resolution.Width = resolution_width;
  384. m_resolution.Height = resolution_height;
  385. if (resolution_width == 0 || resolution_height == 0)
  386. {
  387. m_resolution.Width = pictureBox1.Width;
  388. m_resolution.Height = pictureBox1.Height;
  389. }
  390. }
  391. private byte[] m_pbuf;
  392. /// <summary>
  393. /// 绘制
  394. /// </summary>
  395. /// <param name="pBuf"></param>
  396. /// <param name="obj"></param>
  397. public void CallbackDraw(byte[] pBuf, object obj, int channel)
  398. {
  399. lock (obj)
  400. {
  401. try
  402. {
  403. m_isWaiting = false;
  404. Console.WriteLine("接收全图buf");
  405. m_camera.m_drawAllHandler -= new DrawAllHandler(CallbackDraw);
  406. OpenCvSharp.Mat mat;
  407. if (channel == 1)
  408. {
  409. mat = new OpenCvSharp.Mat(m_resolution.Height, m_resolution.Width, OpenCvSharp.MatType.CV_8UC1, pBuf);
  410. }
  411. else
  412. {
  413. mat = new OpenCvSharp.Mat(m_resolution.Height, m_resolution.Width, OpenCvSharp.MatType.CV_8UC3, pBuf);
  414. }
  415. OpenCvSharp.Cv2.Flip(mat, mat, 0);
  416. this.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
  417. stopDrawing();
  418. if (mat != null)
  419. {
  420. mat.Dispose();
  421. }
  422. GC.Collect();
  423. }
  424. catch (Exception)
  425. {
  426. }
  427. }
  428. }
  429. public void CallMaxDraw(TUCAM_FRAME frame, object obj)
  430. {
  431. m_camera.m_bufHandler -= new BufHandler(CallMaxDraw);
  432. int nSize = (int)(frame.uiImgSize + frame.usHeader);
  433. if (m_pbuf == null || m_pbuf.Length != nSize)
  434. {
  435. m_pbuf = new byte[nSize];
  436. }
  437. Marshal.Copy(frame.pBuffer, m_pbuf, 0, nSize);
  438. Buffer.BlockCopy(m_pbuf, (int)(frame.usHeader), m_pbuf, 0, (int)(frame.uiImgSize));
  439. OpenCvSharp.Mat mat;
  440. if (frame.ucChannels == 1)
  441. {
  442. mat = new OpenCvSharp.Mat(frame.usHeight, frame.usWidth, OpenCvSharp.MatType.CV_8UC1, m_pbuf);
  443. }
  444. else
  445. {
  446. mat = new OpenCvSharp.Mat(frame.usHeight, frame.usWidth, OpenCvSharp.MatType.CV_8UC3, m_pbuf);
  447. }
  448. OpenCvSharp.Cv2.Flip(mat, mat, 0);
  449. this.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
  450. }
  451. private void stopDrawing()
  452. {
  453. //创建一个委托,用于封装一个方法,在这里是封装了 控制更新控件 的方法
  454. Action invokeAction = new Action(stopDrawing);
  455. //判断操作控件的线程是否创建控件的线程
  456. //调用方调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法
  457. if (this.InvokeRequired)
  458. {
  459. //与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用)
  460. this.Invoke(invokeAction);
  461. }
  462. else
  463. {
  464. m_camera.StopDrawing(m_process);
  465. m_camera.StopWaitForFrame();
  466. //窗体线程,即主线程
  467. shuaxinButton.Enabled = true;
  468. }
  469. }
  470. private void shuaxinButton_Click(object sender, EventArgs e)
  471. {
  472. drawingImg();
  473. shuaxinButton.Enabled = false;
  474. }
  475. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  476. {
  477. if (m_isWaiting)
  478. {
  479. return;
  480. }
  481. if (m_rect.Width != pictureBox1.Width || m_rect.Height != pictureBox1.Height)
  482. {
  483. this.quanfuButton.Text = PdnResources.GetString("Menu.Fullframe.text");
  484. }
  485. this.m_cameraParamModel.parame.previewRectX = m_targetX;
  486. this.m_cameraParamModel.parame.previewRectY = m_targetY;
  487. this.m_cameraParamModel.parame.previewRectW = m_targetWidth;
  488. this.m_cameraParamModel.parame.previewRectH = m_targetHeight;
  489. if (m_resolution.Width != 0 && m_resolution.Height != 0)
  490. {
  491. if (m_rect.Width > 0 && m_rect.Height > 0)
  492. {
  493. e.Graphics.DrawRectangle(new Pen(Color.Red, 2), m_rect);//重新绘制颜色为红色
  494. for (int i = 1; i <= 8; i++)
  495. {
  496. e.Graphics.FillRectangle(m_brush, GetHandleRectangle(i));
  497. }
  498. }
  499. }
  500. this.zuobiaoXTextBox.Text = m_targetX.ToString();
  501. this.zuobiaoYTextBox.Text = m_targetY.ToString();
  502. this.chicunWTextBox.Text = m_targetWidth.ToString();
  503. this.chicunHTextBox.Text = m_targetHeight.ToString();
  504. }
  505. private void zuobiaoXTextBox_KeyPress(object sender, KeyPressEventArgs e)
  506. {
  507. if (e.KeyChar == (char)Keys.Enter)
  508. {
  509. int val = Convert.ToInt32(this.zuobiaoXTextBox.Text);
  510. if (val < 0)
  511. {
  512. this.zuobiaoXTextBox.Text = "0";
  513. }
  514. else if ((val + m_targetWidth) > m_resolution.Width)
  515. {
  516. zuobiaoXTextBox.Text = (m_resolution.Width - m_targetWidth).ToString();
  517. }
  518. TextChange();
  519. }
  520. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
  521. {
  522. e.Handled = true;
  523. }
  524. }
  525. private void zuobiaoYTextBox_KeyPress(object sender, KeyPressEventArgs e)
  526. {
  527. if (e.KeyChar == (char)Keys.Enter)
  528. {
  529. int val = Convert.ToInt32(this.zuobiaoYTextBox.Text);
  530. if (val < 0)
  531. {
  532. this.zuobiaoYTextBox.Text = "0";
  533. }
  534. else if ((val + m_targetHeight) > m_resolution.Height)
  535. {
  536. this.zuobiaoYTextBox.Text = (m_resolution.Height - m_targetHeight).ToString();
  537. }
  538. TextChange();
  539. }
  540. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
  541. {
  542. e.Handled = true;
  543. }
  544. }
  545. private void chicunWTextBox_KeyPress(object sender, KeyPressEventArgs e)
  546. {
  547. if (e.KeyChar == (char)Keys.Enter)
  548. {
  549. int val = Convert.ToInt32(this.chicunWTextBox.Text);
  550. if (val < 0)
  551. {
  552. this.chicunWTextBox.Text = "1";
  553. }
  554. else if ((val + Convert.ToInt32(this.zuobiaoXTextBox.Text)) > m_resolution.Width)
  555. {
  556. this.chicunWTextBox.Text = (m_resolution.Width - Convert.ToInt32(this.zuobiaoXTextBox.Text)).ToString();
  557. }
  558. TextChange();
  559. }
  560. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
  561. {
  562. e.Handled = true;
  563. }
  564. }
  565. private void chicunHTextBox_KeyPress(object sender, KeyPressEventArgs e)
  566. {
  567. if (e.KeyChar == (char)Keys.Enter)
  568. {
  569. int val = Convert.ToInt32(this.chicunHTextBox.Text);
  570. if (val < 0)
  571. {
  572. this.chicunHTextBox.Text = "1";
  573. }
  574. else if ((val + Convert.ToInt32(this.zuobiaoYTextBox.Text)) > m_resolution.Height)
  575. {
  576. this.chicunHTextBox.Text = (m_resolution.Height - Convert.ToInt32(this.zuobiaoYTextBox.Text)).ToString();
  577. }
  578. TextChange();
  579. }
  580. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
  581. {
  582. e.Handled = true;
  583. }
  584. }
  585. private void TextChange()
  586. {
  587. m_targetX = Convert.ToInt32(this.zuobiaoXTextBox.Text);
  588. m_targetY = Convert.ToInt32(this.zuobiaoYTextBox.Text);
  589. m_targetWidth = Convert.ToInt32(this.chicunWTextBox.Text);
  590. m_targetHeight = Convert.ToInt32(this.chicunHTextBox.Text);
  591. if (m_resolution.Width > 0 && m_resolution.Height > 0)
  592. {
  593. m_rect.X = m_targetX * this.pictureBox1.Width / m_resolution.Width;
  594. m_rect.Y = m_targetY * this.pictureBox1.Height / m_resolution.Height;
  595. m_rect.Width = m_targetWidth * this.pictureBox1.Width / m_resolution.Width;
  596. m_rect.Height = m_targetHeight * this.pictureBox1.Height / m_resolution.Height;
  597. }
  598. else
  599. {
  600. m_rect.X = m_targetX;
  601. m_rect.Y = m_targetY;
  602. m_rect.Width = m_targetWidth;
  603. m_rect.Height = m_targetHeight;
  604. }
  605. SetROI();
  606. this.pictureBox1.Refresh();
  607. }
  608. private void SetROI()
  609. {
  610. if (m_use)
  611. {
  612. m_camera.SetROI(ref m_targetX, ref m_targetY, ref m_targetWidth, ref m_targetHeight);
  613. this.m_cameraParamModel.parame.previewRectX = m_targetX;
  614. this.m_cameraParamModel.parame.previewRectY = m_targetY;
  615. this.m_cameraParamModel.parame.previewRectW = m_targetWidth;
  616. this.m_cameraParamModel.parame.previewRectH = m_targetHeight;
  617. }
  618. }
  619. private void cmbResolution_SelectedIndexChanged(object sender, EventArgs e)
  620. {
  621. m_cameraParamModel.parame.Resolution = cmbResolution.SelectedIndex;
  622. int resolutionId = m_camera.GetResolutionId();
  623. if (resolutionId != m_cameraParamModel.parame.Resolution)
  624. {
  625. // 分辨率
  626. m_camera.SetResolution(m_cameraParamModel.parame.Resolution);
  627. //m_camera.m_bufHandler += new BufHandler(CallMaxDraw);
  628. }
  629. m_camera.GetROI(ref m_cameraParamModel.parame.previewRectX, ref m_cameraParamModel.parame.previewRectY, ref m_cameraParamModel.parame.previewRectW, ref m_cameraParamModel.parame.previewRectH);
  630. InitializeData();
  631. this.zuobiaoXTextBox.Text = m_cameraParamModel.parame.previewRectX.ToString();
  632. this.zuobiaoYTextBox.Text = m_cameraParamModel.parame.previewRectY.ToString();
  633. this.chicunWTextBox.Text = m_cameraParamModel.parame.previewRectW.ToString();
  634. this.chicunHTextBox.Text = m_cameraParamModel.parame.previewRectH.ToString();
  635. }
  636. private void chicunHTextBox_Leave(object sender, EventArgs e)
  637. {
  638. int val = Convert.ToInt32(this.chicunHTextBox.Text);
  639. if (val < 0)
  640. {
  641. this.chicunHTextBox.Text = "1";
  642. }
  643. else if ((val + Convert.ToInt32(this.zuobiaoYTextBox.Text)) > m_resolution.Height)
  644. {
  645. this.chicunHTextBox.Text = (m_resolution.Height - Convert.ToInt32(this.zuobiaoYTextBox.Text)).ToString();
  646. }
  647. TextChange();
  648. }
  649. private void chicunWTextBox_Leave(object sender, EventArgs e)
  650. {
  651. int val = Convert.ToInt32(this.chicunWTextBox.Text);
  652. if (val < 0)
  653. {
  654. this.chicunWTextBox.Text = "1";
  655. }
  656. else if ((val + Convert.ToInt32(this.zuobiaoXTextBox.Text)) > m_resolution.Width)
  657. {
  658. this.chicunWTextBox.Text = (m_resolution.Width - Convert.ToInt32(this.zuobiaoXTextBox.Text)).ToString();
  659. }
  660. TextChange();
  661. }
  662. private void zuobiaoYTextBox_Leave(object sender, EventArgs e)
  663. {
  664. int val = Convert.ToInt32(this.zuobiaoYTextBox.Text);
  665. if (val < 0)
  666. {
  667. this.zuobiaoYTextBox.Text = "0";
  668. }
  669. else if ((val + m_targetHeight) > m_resolution.Height)
  670. {
  671. this.zuobiaoYTextBox.Text = (m_resolution.Height - m_targetHeight).ToString();
  672. }
  673. TextChange();
  674. }
  675. private void zuobiaoXTextBox_Leave(object sender, EventArgs e)
  676. {
  677. int val = Convert.ToInt32(this.zuobiaoXTextBox.Text);
  678. if (val < 0)
  679. {
  680. this.zuobiaoXTextBox.Text = "0";
  681. }
  682. else if ((val + m_targetWidth) > m_resolution.Width)
  683. {
  684. zuobiaoXTextBox.Text = (m_resolution.Width - m_targetWidth).ToString();
  685. }
  686. TextChange();
  687. }
  688. }
  689. }