FrameCameraSetControl.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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 FrameCameraSetControl : 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 FrameCameraSetControl(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(Bitmap bitmap)
  430. {
  431. m_camera.m_bufHandler -= CallMaxDraw;
  432. this.pictureBox1.Image = bitmap;
  433. }
  434. private void stopDrawing()
  435. {
  436. //创建一个委托,用于封装一个方法,在这里是封装了 控制更新控件 的方法
  437. Action invokeAction = new Action(stopDrawing);
  438. //判断操作控件的线程是否创建控件的线程
  439. //调用方调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法
  440. if (this.InvokeRequired)
  441. {
  442. //与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用)
  443. this.Invoke(invokeAction);
  444. }
  445. else
  446. {
  447. m_camera.StopDrawing(m_process);
  448. m_camera.StopWaitForFrame();
  449. //窗体线程,即主线程
  450. shuaxinButton.Enabled = true;
  451. }
  452. }
  453. private void shuaxinButton_Click(object sender, EventArgs e)
  454. {
  455. drawingImg();
  456. shuaxinButton.Enabled = false;
  457. }
  458. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  459. {
  460. if (m_isWaiting)
  461. {
  462. return;
  463. }
  464. if (m_rect.Width != pictureBox1.Width || m_rect.Height != pictureBox1.Height)
  465. {
  466. this.quanfuButton.Text = PdnResources.GetString("Menu.Fullframe.text");
  467. }
  468. this.m_cameraParamModel.parame.previewRectX = m_targetX;
  469. this.m_cameraParamModel.parame.previewRectY = m_targetY;
  470. this.m_cameraParamModel.parame.previewRectW = m_targetWidth;
  471. this.m_cameraParamModel.parame.previewRectH = m_targetHeight;
  472. if (m_resolution.Width != 0 && m_resolution.Height != 0)
  473. {
  474. if (m_rect.Width > 0 && m_rect.Height > 0)
  475. {
  476. e.Graphics.DrawRectangle(new Pen(Color.Red, 2), m_rect);//重新绘制颜色为红色
  477. for (int i = 1; i <= 8; i++)
  478. {
  479. e.Graphics.FillRectangle(m_brush, GetHandleRectangle(i));
  480. }
  481. }
  482. }
  483. this.zuobiaoXTextBox.Text = m_targetX.ToString();
  484. this.zuobiaoYTextBox.Text = m_targetY.ToString();
  485. this.chicunWTextBox.Text = m_targetWidth.ToString();
  486. this.chicunHTextBox.Text = m_targetHeight.ToString();
  487. }
  488. private void zuobiaoXTextBox_KeyPress(object sender, KeyPressEventArgs e)
  489. {
  490. if (e.KeyChar == (char)Keys.Enter)
  491. {
  492. int val = Convert.ToInt32(this.zuobiaoXTextBox.Text);
  493. if (val < 0)
  494. {
  495. this.zuobiaoXTextBox.Text = "0";
  496. }
  497. else if ((val + m_targetWidth) > m_resolution.Width)
  498. {
  499. zuobiaoXTextBox.Text = (m_resolution.Width - m_targetWidth).ToString();
  500. }
  501. TextChange();
  502. }
  503. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
  504. {
  505. e.Handled = true;
  506. }
  507. }
  508. private void zuobiaoYTextBox_KeyPress(object sender, KeyPressEventArgs e)
  509. {
  510. if (e.KeyChar == (char)Keys.Enter)
  511. {
  512. int val = Convert.ToInt32(this.zuobiaoYTextBox.Text);
  513. if (val < 0)
  514. {
  515. this.zuobiaoYTextBox.Text = "0";
  516. }
  517. else if ((val + m_targetHeight) > m_resolution.Height)
  518. {
  519. this.zuobiaoYTextBox.Text = (m_resolution.Height - m_targetHeight).ToString();
  520. }
  521. TextChange();
  522. }
  523. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
  524. {
  525. e.Handled = true;
  526. }
  527. }
  528. private void chicunWTextBox_KeyPress(object sender, KeyPressEventArgs e)
  529. {
  530. if (e.KeyChar == (char)Keys.Enter)
  531. {
  532. int val = Convert.ToInt32(this.chicunWTextBox.Text);
  533. if (val < 0)
  534. {
  535. this.chicunWTextBox.Text = "1";
  536. }
  537. else if ((val + Convert.ToInt32(this.zuobiaoXTextBox.Text)) > m_resolution.Width)
  538. {
  539. this.chicunWTextBox.Text = (m_resolution.Width - Convert.ToInt32(this.zuobiaoXTextBox.Text)).ToString();
  540. }
  541. TextChange();
  542. }
  543. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
  544. {
  545. e.Handled = true;
  546. }
  547. }
  548. private void chicunHTextBox_KeyPress(object sender, KeyPressEventArgs e)
  549. {
  550. if (e.KeyChar == (char)Keys.Enter)
  551. {
  552. int val = Convert.ToInt32(this.chicunHTextBox.Text);
  553. if (val < 0)
  554. {
  555. this.chicunHTextBox.Text = "1";
  556. }
  557. else if ((val + Convert.ToInt32(this.zuobiaoYTextBox.Text)) > m_resolution.Height)
  558. {
  559. this.chicunHTextBox.Text = (m_resolution.Height - Convert.ToInt32(this.zuobiaoYTextBox.Text)).ToString();
  560. }
  561. TextChange();
  562. }
  563. if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
  564. {
  565. e.Handled = true;
  566. }
  567. }
  568. private void TextChange()
  569. {
  570. m_targetX = Convert.ToInt32(this.zuobiaoXTextBox.Text);
  571. m_targetY = Convert.ToInt32(this.zuobiaoYTextBox.Text);
  572. m_targetWidth = Convert.ToInt32(this.chicunWTextBox.Text);
  573. m_targetHeight = Convert.ToInt32(this.chicunHTextBox.Text);
  574. if (m_resolution.Width > 0 && m_resolution.Height > 0)
  575. {
  576. m_rect.X = m_targetX * this.pictureBox1.Width / m_resolution.Width;
  577. m_rect.Y = m_targetY * this.pictureBox1.Height / m_resolution.Height;
  578. m_rect.Width = m_targetWidth * this.pictureBox1.Width / m_resolution.Width;
  579. m_rect.Height = m_targetHeight * this.pictureBox1.Height / m_resolution.Height;
  580. }
  581. else
  582. {
  583. m_rect.X = m_targetX;
  584. m_rect.Y = m_targetY;
  585. m_rect.Width = m_targetWidth;
  586. m_rect.Height = m_targetHeight;
  587. }
  588. SetROI();
  589. this.pictureBox1.Refresh();
  590. }
  591. private void SetROI()
  592. {
  593. if (m_use)
  594. {
  595. m_camera.SetROI(ref m_targetX, ref m_targetY, ref m_targetWidth, ref m_targetHeight);
  596. this.m_cameraParamModel.parame.previewRectX = m_targetX;
  597. this.m_cameraParamModel.parame.previewRectY = m_targetY;
  598. this.m_cameraParamModel.parame.previewRectW = m_targetWidth;
  599. this.m_cameraParamModel.parame.previewRectH = m_targetHeight;
  600. }
  601. }
  602. private void cmbResolution_SelectedIndexChanged(object sender, EventArgs e)
  603. {
  604. m_cameraParamModel.parame.Resolution = cmbResolution.SelectedIndex;
  605. int resolutionId = m_camera.GetResolutionId();
  606. if (resolutionId != m_cameraParamModel.parame.Resolution)
  607. {
  608. // 分辨率
  609. m_camera.SetResolution(m_cameraParamModel.parame.Resolution);
  610. //m_camera.m_bufHandler += new BufHandler(CallMaxDraw);
  611. }
  612. m_camera.GetROI(ref m_cameraParamModel.parame.previewRectX, ref m_cameraParamModel.parame.previewRectY, ref m_cameraParamModel.parame.previewRectW, ref m_cameraParamModel.parame.previewRectH);
  613. InitializeData();
  614. this.zuobiaoXTextBox.Text = m_cameraParamModel.parame.previewRectX.ToString();
  615. this.zuobiaoYTextBox.Text = m_cameraParamModel.parame.previewRectY.ToString();
  616. this.chicunWTextBox.Text = m_cameraParamModel.parame.previewRectW.ToString();
  617. this.chicunHTextBox.Text = m_cameraParamModel.parame.previewRectH.ToString();
  618. }
  619. private void chicunHTextBox_Leave(object sender, EventArgs e)
  620. {
  621. int val = Convert.ToInt32(this.chicunHTextBox.Text);
  622. if (val < 0)
  623. {
  624. this.chicunHTextBox.Text = "1";
  625. }
  626. else if ((val + Convert.ToInt32(this.zuobiaoYTextBox.Text)) > m_resolution.Height)
  627. {
  628. this.chicunHTextBox.Text = (m_resolution.Height - Convert.ToInt32(this.zuobiaoYTextBox.Text)).ToString();
  629. }
  630. TextChange();
  631. }
  632. private void chicunWTextBox_Leave(object sender, EventArgs e)
  633. {
  634. int val = Convert.ToInt32(this.chicunWTextBox.Text);
  635. if (val < 0)
  636. {
  637. this.chicunWTextBox.Text = "1";
  638. }
  639. else if ((val + Convert.ToInt32(this.zuobiaoXTextBox.Text)) > m_resolution.Width)
  640. {
  641. this.chicunWTextBox.Text = (m_resolution.Width - Convert.ToInt32(this.zuobiaoXTextBox.Text)).ToString();
  642. }
  643. TextChange();
  644. }
  645. private void zuobiaoYTextBox_Leave(object sender, EventArgs e)
  646. {
  647. int val = Convert.ToInt32(this.zuobiaoYTextBox.Text);
  648. if (val < 0)
  649. {
  650. this.zuobiaoYTextBox.Text = "0";
  651. }
  652. else if ((val + m_targetHeight) > m_resolution.Height)
  653. {
  654. this.zuobiaoYTextBox.Text = (m_resolution.Height - m_targetHeight).ToString();
  655. }
  656. TextChange();
  657. }
  658. private void zuobiaoXTextBox_Leave(object sender, EventArgs e)
  659. {
  660. int val = Convert.ToInt32(this.zuobiaoXTextBox.Text);
  661. if (val < 0)
  662. {
  663. this.zuobiaoXTextBox.Text = "0";
  664. }
  665. else if ((val + m_targetWidth) > m_resolution.Width)
  666. {
  667. zuobiaoXTextBox.Text = (m_resolution.Width - m_targetWidth).ToString();
  668. }
  669. TextChange();
  670. }
  671. }
  672. }