123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using PaintDotNet.Base.SettingModel;
- using System.Collections;
- using PaintDotNet.ImageCollect;
- using TUCamera;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Threading;
- namespace PaintDotNet.Setting.LabelComponent
- {
- /// <summary>
- /// 相机设置,边框界面
- /// </summary>
- public class FrameCameraControl2 : UserControl
- {
- #region 控件
- private GroupBox groupBox3;
- public PictureBox pictureBox1;
- private Button shuaxinButton;
- private Label label1;
- private TextBox zuobiaoXTextBox;
- private TextBox zuobiaoYTextBox;
- private Label label2;
- private TextBox chicunHTextBox;
- private TextBox chicunWTextBox;
- private Label label4;
- private Button quanfuButton;
- private Button zhongxinButton;
- private Label label3;
- #endregion
- /// <summary>
- /// 相机参数的Model
- /// </summary>
- private CameraParamModel m_cameraParamModel;
- /// <summary>
- /// 相机设置的dialog
- /// </summary>
- private Point m_startP; //画框的起始点
- private bool m_blnDraw = false;
- private Rectangle m_rect ;//初始设置一个拖拽的显示框
- SolidBrush m_brush = new SolidBrush(Color.Black);
- int m_handle = 0;
- int m_targetX = 0; // 起始点X坐标 对应分辨率
- int m_targetY = 0; // 起始点Y坐标 对应分辨率
- int m_targetWidth = 320; //显示框的宽度 对应分辨率
- int m_targetHeight = 200; //显示框的高度 对应分辨率
- int m_tempX = 0;
- int m_tempY = 0;
- int m_tempW = 0;
- int m_tempH = 0;
- private TUCameraIFrameProcess m_process = null;
- private Size m_resolution; // 分辨率
- private TUCamera.TUCamera m_camera;
- private bool m_use = false; // 设置是否立刻生效,设置到相机
- private OpenCvSharp.Mat m_mat;
-
- string[] m_binArray = new string[] { "1x1 Bin", "2x2 Bin", "3x3 Bin", "4x4 Bin" };
- private bool m_isWaiting;
- private int m_selectedBin;
- public FrameCameraControl2(CameraParamModel model , bool use)
- {
- this.m_use = use;
- this.m_cameraParamModel = model;
- this.m_camera = TUCameraManager.GetInstance().GetCurrentCamera();
- InitializeComponent();
- InitializeLanguageText();
- InitializeControlData();
- }
- /// <summary>
- /// 设置下拉等数据源
- /// </summary>
- private void InitializeControlData()
- {
- this.zuobiaoXTextBox.Text = m_cameraParamModel.parame.previewRectX.ToString();
- this.zuobiaoYTextBox.Text = m_cameraParamModel.parame.previewRectY.ToString();
- this.chicunWTextBox.Text = m_cameraParamModel.parame.previewRectW.ToString();
- this.chicunHTextBox.Text = m_cameraParamModel.parame.previewRectH.ToString();
- }
- /// <summary>
- /// 绑定样式数据
- /// </summary>
- private void InitializeData()
- {
- m_targetX = this.m_cameraParamModel.parame.previewRectX;
- m_targetY = this.m_cameraParamModel.parame.previewRectY;
- m_targetWidth = this.m_cameraParamModel.parame.previewRectW;
- m_targetHeight = this.m_cameraParamModel.parame.previewRectH;
- SetROI();
- m_rect = new Rectangle();
- drawingImg();
- if (m_resolution.Width > 0 && m_resolution.Height > 0)
- {
- this.pictureBox1.Height = (this.pictureBox1.Width * m_resolution.Height) / m_resolution.Width;
- m_rect.X = this.m_cameraParamModel.parame.previewRectX * this.pictureBox1.Width / m_resolution.Width;
- m_rect.Y = this.m_cameraParamModel.parame.previewRectY * this.pictureBox1.Height / m_resolution.Height;
- m_rect.Width = this.m_cameraParamModel.parame.previewRectW * this.pictureBox1.Width / m_resolution.Width;
- m_rect.Height = this.m_cameraParamModel.parame.previewRectH * this.pictureBox1.Height / m_resolution.Height;
- Console.WriteLine(m_rect);
- }
- else
- {
- m_rect.X = 0;
- m_rect.Y = 0;
- m_rect.Width = this.pictureBox1.Width;
- m_rect.Height = this.pictureBox1.Height;
- }
- }
- private void InitializeLanguageText()
- {
- this.groupBox3.Text = PdnResources.GetString("Menu.Setting.Text");
- this.quanfuButton.Text = PdnResources.GetString("Menu.Fullframe.text");
- this.zhongxinButton.Text = PdnResources.GetString("Menu.center.text");
- this.label4.Text = PdnResources.GetString("Menu.Imagesize.text")+":";
- this.label1.Text = PdnResources.GetString("Menu.Startingcoordinates.text")+":";
- this.shuaxinButton.Text = PdnResources.GetString("Menu.Refreshthumbnail.text");
- }
-
- private void InitializeComponent()
- {
- this.groupBox3 = new System.Windows.Forms.GroupBox();
- this.label3 = new System.Windows.Forms.Label();
- this.quanfuButton = new System.Windows.Forms.Button();
- this.zhongxinButton = new System.Windows.Forms.Button();
- this.chicunHTextBox = new System.Windows.Forms.TextBox();
- this.chicunWTextBox = new System.Windows.Forms.TextBox();
- this.label4 = new System.Windows.Forms.Label();
- this.zuobiaoYTextBox = new System.Windows.Forms.TextBox();
- this.label2 = new System.Windows.Forms.Label();
- this.zuobiaoXTextBox = new System.Windows.Forms.TextBox();
- this.label1 = new System.Windows.Forms.Label();
- this.shuaxinButton = new System.Windows.Forms.Button();
- this.pictureBox1 = new System.Windows.Forms.PictureBox();
- this.groupBox3.SuspendLayout();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
- this.SuspendLayout();
- //
- // groupBox3
- //
- this.groupBox3.Controls.Add(this.label3);
- this.groupBox3.Controls.Add(this.quanfuButton);
- this.groupBox3.Controls.Add(this.zhongxinButton);
- this.groupBox3.Controls.Add(this.chicunHTextBox);
- this.groupBox3.Controls.Add(this.chicunWTextBox);
- this.groupBox3.Controls.Add(this.label4);
- this.groupBox3.Controls.Add(this.zuobiaoYTextBox);
- this.groupBox3.Controls.Add(this.label2);
- this.groupBox3.Controls.Add(this.zuobiaoXTextBox);
- this.groupBox3.Controls.Add(this.label1);
- this.groupBox3.Controls.Add(this.shuaxinButton);
- this.groupBox3.Controls.Add(this.pictureBox1);
- this.groupBox3.Location = new System.Drawing.Point(12, 12);
- this.groupBox3.Name = "groupBox3";
- this.groupBox3.Size = new System.Drawing.Size(464, 378);
- this.groupBox3.TabIndex = 2;
- this.groupBox3.TabStop = false;
- this.groupBox3.Text = "设置";
- //
- // label3
- //
- this.label3.AutoSize = true;
- this.label3.Location = new System.Drawing.Point(176, 329);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(17, 12);
- this.label3.TabIndex = 12;
- this.label3.Text = "×";
- //
- // quanfuButton
- //
- this.quanfuButton.Location = new System.Drawing.Point(298, 325);
- this.quanfuButton.Name = "quanfuButton";
- this.quanfuButton.Size = new System.Drawing.Size(75, 23);
- this.quanfuButton.TabIndex = 11;
- this.quanfuButton.Text = "全幅";
- this.quanfuButton.UseVisualStyleBackColor = true;
- this.quanfuButton.Click += new System.EventHandler(this.button3_Click);
- //
- // zhongxinButton
- //
- this.zhongxinButton.Location = new System.Drawing.Point(298, 288);
- this.zhongxinButton.Name = "zhongxinButton";
- this.zhongxinButton.Size = new System.Drawing.Size(75, 23);
- this.zhongxinButton.TabIndex = 10;
- this.zhongxinButton.Text = "中心";
- this.zhongxinButton.UseVisualStyleBackColor = true;
- this.zhongxinButton.Click += new System.EventHandler(this.button2_Click);
- //
- // chicunHTextBox
- //
- this.chicunHTextBox.Location = new System.Drawing.Point(208, 325);
- this.chicunHTextBox.Name = "chicunHTextBox";
- this.chicunHTextBox.Size = new System.Drawing.Size(62, 21);
- this.chicunHTextBox.TabIndex = 9;
- this.chicunHTextBox.Text = "100";
- this.chicunHTextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.chicunHTextBox_KeyPress);
- this.chicunHTextBox.Leave += new System.EventHandler(this.chicunHTextBox_Leave);
- //
- // chicunWTextBox
- //
- this.chicunWTextBox.Location = new System.Drawing.Point(99, 325);
- this.chicunWTextBox.Name = "chicunWTextBox";
- this.chicunWTextBox.Size = new System.Drawing.Size(62, 21);
- this.chicunWTextBox.TabIndex = 7;
- this.chicunWTextBox.Text = "100";
- this.chicunWTextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.chicunWTextBox_KeyPress);
- this.chicunWTextBox.Leave += new System.EventHandler(this.chicunWTextBox_Leave);
- //
- // label4
- //
- this.label4.AutoSize = true;
- this.label4.Location = new System.Drawing.Point(19, 325);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(65, 12);
- this.label4.TabIndex = 6;
- this.label4.Text = "图像尺寸:";
- //
- // zuobiaoYTextBox
- //
- this.zuobiaoYTextBox.Location = new System.Drawing.Point(208, 291);
- this.zuobiaoYTextBox.Name = "zuobiaoYTextBox";
- this.zuobiaoYTextBox.Size = new System.Drawing.Size(62, 21);
- this.zuobiaoYTextBox.TabIndex = 5;
- this.zuobiaoYTextBox.Text = "100";
- this.zuobiaoYTextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.zuobiaoYTextBox_KeyPress);
- this.zuobiaoYTextBox.Leave += new System.EventHandler(this.zuobiaoYTextBox_Leave);
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.Location = new System.Drawing.Point(176, 294);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(17, 12);
- this.label2.TabIndex = 4;
- this.label2.Text = "×";
- //
- // zuobiaoXTextBox
- //
- this.zuobiaoXTextBox.Location = new System.Drawing.Point(99, 291);
- this.zuobiaoXTextBox.Name = "zuobiaoXTextBox";
- this.zuobiaoXTextBox.Size = new System.Drawing.Size(62, 21);
- this.zuobiaoXTextBox.TabIndex = 3;
- this.zuobiaoXTextBox.Text = "100";
- this.zuobiaoXTextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.zuobiaoXTextBox_KeyPress);
- this.zuobiaoXTextBox.Leave += new System.EventHandler(this.zuobiaoXTextBox_Leave);
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Location = new System.Drawing.Point(19, 294);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(65, 12);
- this.label1.TabIndex = 2;
- this.label1.Text = "起始坐标:";
- //
- // shuaxinButton
- //
- this.shuaxinButton.Location = new System.Drawing.Point(110, 250);
- this.shuaxinButton.Name = "shuaxinButton";
- this.shuaxinButton.Size = new System.Drawing.Size(242, 23);
- this.shuaxinButton.TabIndex = 1;
- this.shuaxinButton.Text = "刷新缩略图";
- this.shuaxinButton.UseVisualStyleBackColor = true;
- this.shuaxinButton.Click += new System.EventHandler(this.button1_Click);
- //
- // pictureBox1
- //
- this.pictureBox1.BackColor = System.Drawing.Color.White;
- this.pictureBox1.Location = new System.Drawing.Point(110, 33);
- this.pictureBox1.Name = "pictureBox1";
- this.pictureBox1.Size = new System.Drawing.Size(242, 200);
- this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
- this.pictureBox1.TabIndex = 0;
- this.pictureBox1.TabStop = false;
- this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.imageBox1_Paint);
- this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseDown);
- this.pictureBox1.MouseHover += new System.EventHandler(this.PictureBox1_MouseHover);
- this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseMove);
- this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseUp);
- //
- // FrameCameraControl2
- //
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.Controls.Add(this.groupBox3);
- this.Name = "FrameCameraControl2";
- this.Size = new System.Drawing.Size(488, 405);
- this.groupBox3.ResumeLayout(false);
- this.groupBox3.PerformLayout();
- ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
- this.ResumeLayout(false);
- }
- //中心按钮被点击
- private void button2_Click(object sender, EventArgs e)
- {
- if (zhongxinButton.Text.Equals(PdnResources.GetString("Menu.center.text")))
- {
- zhongxinButton.Text = PdnResources.GetString("Menu.Start.text");
- int X = pictureBox1.Width / 2 - m_rect.Width / 2;
- int Y = pictureBox1.Height / 2 - m_rect.Height / 2;
- m_rect.X = X;
- m_rect.Y = Y;
- m_targetX = m_rect.X * m_resolution.Width / this.pictureBox1.Width;
- m_targetY = m_rect.Y * m_resolution.Height / this.pictureBox1.Height;
- }
- else
- {
- zhongxinButton.Text = PdnResources.GetString("Menu.center.text");
- int X = 0;
- int Y = 0;
- m_rect.X = X;
- m_rect.Y = Y;
- m_targetX = m_rect.X * m_resolution.Width / this.pictureBox1.Width;
- m_targetY = m_rect.Y * m_resolution.Height / this.pictureBox1.Height;
- }
- SetROI();
- this.pictureBox1.Refresh();
- }
- //点击上次,全幅
- private void button3_Click(object sender, EventArgs e)
- {
- if (this.quanfuButton.Text.Equals(PdnResources.GetString("Menu.Lasttime.text")))
- {
- this.quanfuButton.Text = PdnResources.GetString("Menu.Fullframe.text");
- m_rect.X = m_tempX * this.pictureBox1.Width / m_resolution.Width;
- m_rect.Y = m_tempY * this.pictureBox1.Height / m_resolution.Height;
- m_rect.Width = m_tempW * this.pictureBox1.Width / m_resolution.Width;
- m_rect.Height = m_tempH * this.pictureBox1.Height / m_resolution.Height;
- m_targetX = m_tempX;
- m_targetY = m_tempY;
- m_targetWidth = m_tempW;
- m_targetHeight = m_tempH;
- }
- else {
- this.quanfuButton.Text = PdnResources.GetString("Menu.Lasttime.text");
- m_tempX = m_targetX;
- m_tempY = m_targetY;
- m_tempW = m_targetWidth;
- m_tempH = m_targetHeight;
- int X = 0;
- int Y = 0;
- m_rect.X = X;
- m_rect.Y = Y;
- m_rect.Width = this.pictureBox1.Width;
- m_rect.Height = this.pictureBox1.Height;
- m_targetX = 0;
- m_targetY = 0;
- m_targetWidth = m_resolution.Width;
- m_targetHeight = m_resolution.Height;
- }
- SetROI();
- this.pictureBox1.Refresh();
- }
- private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
- {
- m_handle = HitTest(e.Location);
- if (m_handle < 0)
- {
- if (m_rect.Contains(e.Location))//判断鼠标按下的坐标是否在红框中,确定是否拖动的红框
- {
- m_handle = 9;
- }
- }
- this.Cursor = GetHandleCursor(m_handle);
- m_startP = e.Location;
- Invalidate();
- m_blnDraw = true;
- }
- private void PictureBox1_MouseHover(object sender, EventArgs e)
- {
- }
- private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
- {
- if (m_handle>0 && m_blnDraw)
- {
- if (e.Button != MouseButtons.Left)//判断是否按下左键
- return;
- MoveHandleTo(e.Location, m_handle);
- }
- }
- private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
- {
- m_blnDraw = false; //结束绘制
- this.Cursor = Cursors.Default;
- SetROI();
- this.pictureBox1.Refresh();
- }
- private void imageBox1_Paint(object sender, PaintEventArgs e)
- {
- if (m_isWaiting)
- {
- return;
- }
- if (m_rect.Width != pictureBox1.Width || m_rect.Height != pictureBox1.Height)
- {
- this.quanfuButton.Text = PdnResources.GetString("Menu.Fullframe.text");
- }
- this.m_cameraParamModel.parame.previewRectX = m_targetX;
- this.m_cameraParamModel.parame.previewRectY = m_targetY;
- this.m_cameraParamModel.parame.previewRectW = m_targetWidth;
- this.m_cameraParamModel.parame.previewRectH = m_targetHeight;
- if(m_resolution.Width != 0 && m_resolution.Height != 0)
- {
- if (m_rect.Width > 0 && m_rect.Height > 0)
- {
- e.Graphics.DrawRectangle(new Pen(Color.Red, 2), m_rect);//重新绘制颜色为红色
- for (int i = 1; i <= 8; i++)
- {
- e.Graphics.FillRectangle(m_brush, GetHandleRectangle(i));
- }
- }
- }
- this.zuobiaoXTextBox.Text = m_targetX.ToString();
- this.zuobiaoYTextBox.Text = m_targetY.ToString();
- this.chicunWTextBox.Text = m_targetWidth.ToString();
- this.chicunHTextBox.Text = m_targetHeight.ToString();
- }
- public Rectangle GetHandleRectangle(int handleNumber)
- {
- Point point = GetHandle(handleNumber);
- return new Rectangle(point.X - 4, point.Y - 4, 8, 8);
- }
- public int HitTest(Point point)
- {
- for (int i = 1; i <= 8; i++)
- {
- if (GetHandleRectangle(i).Contains(point))
- return i;
- }
- return -1;
- }
- public Point GetHandle(int handleNumber)
- {
- int x, y, xCenter, yCenter;
-
- xCenter = m_rect.X + m_rect.Width / 2;
- yCenter = m_rect.Y + m_rect.Height / 2;
- x = m_rect.X;
- y = m_rect.Y;
-
- switch (handleNumber)
- {
- case 1:
- x = m_rect.X;
- y = m_rect.Y;
- break;
- case 2:
- x = xCenter;
- y = m_rect.Y;
- break;
- case 3:
- x = m_rect.Right;
- y = m_rect.Y;
- break;
- case 4:
- x = m_rect.Right;
- y = yCenter;
- break;
- case 5:
- x = m_rect.Right;
- y = m_rect.Bottom;
- break;
- case 6:
- x = xCenter;
- y = m_rect.Bottom;
- break;
- case 7:
- x = m_rect.X;
- y = m_rect.Bottom;
- break;
- case 8:
- x = m_rect.X;
- y = yCenter;
- break;
- }
- return new Point(x, y);
- }
- public Cursor GetHandleCursor(int handleNumber)
- {
- switch (handleNumber)
- {
- case 1:
- return Cursors.SizeNWSE;
- case 2:
- return Cursors.SizeNS;
- case 3:
- return Cursors.SizeNESW;
- case 4:
- return Cursors.SizeWE;
- case 5:
- return Cursors.SizeNWSE;
- case 6:
- return Cursors.SizeNS;
- case 7:
- return Cursors.SizeNESW;
- case 8:
- return Cursors.SizeWE;
- default:
- return Cursors.Default;
- }
- }
- public void MoveHandleTo(Point point, int handleNumber)
- {
- if (point.X > pictureBox1.Width)
- {
- point.X = pictureBox1.Width;
- }
- if (point.X < 0)
- {
- point.X = 0;
- }
- if (point.Y < 0)
- {
- point.Y = 0;
- }
- if (point.Y > pictureBox1.Height)
- {
- point.Y = pictureBox1.Height;
- }
- int left = m_rect.Left;
- int top = m_rect.Top;
- int right = m_rect.Right;
- int bottom = m_rect.Bottom;
- switch (handleNumber)
- {
- case 1:
- left = point.X;
- top = point.Y;
- break;
- case 2:
- top = point.Y;
- break;
- case 3:
- right = point.X;
- top = point.Y;
- break;
- case 4:
- right = point.X;
- break;
- case 5:
- right = point.X;
- bottom = point.Y;
- break;
- case 6:
- bottom = point.Y;
- break;
- case 7:
- left = point.X;
- bottom = point.Y;
- break;
- case 8:
- left = point.X;
- break;
- case 9:
- left = m_rect.Left + (point.X - m_startP.X);
- top = m_rect.Top + (point.Y - m_startP.Y);
- //判断是否超过左上角
- if (left < 0)
- left = 0;
- if (top < 0)
- top = 0;
- //判断是否超过右下 角
- if (left > (pictureBox1.Width - m_rect.Width))
- left = pictureBox1.Width - m_rect.Width;
- if (top > (pictureBox1.Height - m_rect.Height))
- top = pictureBox1.Height - m_rect.Height;
- right = left + m_rect.Width;
- bottom = top + m_rect.Height;
- break;
- }
- m_rect.X = left;
- m_rect.Y = top;
- m_rect.Width = right - left;
- m_rect.Height = bottom - top;
- m_targetX = m_rect.X * m_resolution.Width / this.pictureBox1.Width;
- m_targetY = m_rect.Y * m_resolution.Height / this.pictureBox1.Height;
- m_targetWidth = m_rect.Width * m_resolution.Width / this.pictureBox1.Width;
- m_targetHeight = m_rect.Height * m_resolution.Height / this.pictureBox1.Height;
- pictureBox1.Refresh();
- m_startP = point;
- }
- /// <summary>
- /// 刷新缩略图
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button1_Click(object sender, EventArgs e)
- {
- drawingImg();
- shuaxinButton.Enabled = false;
- }
- private void drawingImg()
- {
- Console.WriteLine("点击刷新缩略图");
- m_isWaiting = true;
- int resolution_width = pictureBox1.Width;
- int resolution_height = pictureBox1.Height;
-
- if (m_camera.IsOpen())
- {
- m_camera.m_drawAllHandler += new DrawAllHandler(CallbackDraw);
- m_camera.StartWaitForFrame();
- m_process = m_camera.StartDrawing(ref resolution_width, ref resolution_height);
- }
- m_resolution.Width = resolution_width;
- m_resolution.Height = resolution_height;
- if (resolution_width == 0 || resolution_height == 0)
- {
- m_resolution.Width = pictureBox1.Width;
- m_resolution.Height = pictureBox1.Height;
- }
- }
- /// <summary>
- /// 绘制
- /// </summary>
- /// <param name="pBuf"></param>
- /// <param name="obj"></param>
- public void CallbackDraw(byte[] pBuf, object obj, int channel)
- {
- lock (obj)
- {
- m_isWaiting = false;
- Console.WriteLine("接收全图buf");
- m_camera.m_drawAllHandler -= new DrawAllHandler(CallbackDraw);
- OpenCvSharp.Mat mat;
- if (channel == 1)
- {
- mat = new OpenCvSharp.Mat(m_resolution.Height, m_resolution.Width, OpenCvSharp.MatType.CV_8UC1, pBuf);
- }
- else
- {
- mat = new OpenCvSharp.Mat(m_resolution.Height, m_resolution.Width, OpenCvSharp.MatType.CV_8UC3, pBuf);
- }
- OpenCvSharp.Cv2.Flip(mat, mat, 0);
- this.pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat);
- stopDrawing();
- if (mat != null)
- {
- mat.Dispose();
- }
- if (m_mat != null)
- {
- m_mat.Dispose();
- }
-
- }
- }
- private void stopDrawing()
- {
- //创建一个委托,用于封装一个方法,在这里是封装了 控制更新控件 的方法
- Action invokeAction = new Action(stopDrawing);
- //判断操作控件的线程是否创建控件的线程
- //调用方调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法
- if (this.InvokeRequired)
- {
- //与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用)
- this.Invoke(invokeAction);
- }
- else
- {
- m_camera.StopDrawing(m_process);
- m_camera.StopWaitForFrame();
- //窗体线程,即主线程
- shuaxinButton.Enabled = true;
- }
- }
- /// <summary>
- /// 黑白模式被点击
- /// </summary>
- private void heibaiRadioButton_CheckedChanged(object sender, EventArgs e)
- {
- RadioButton rb = (RadioButton)sender;
- if (rb.Checked) {
- m_cameraParamModel.parame.Monochromatic = 1;
- // 设置到相机
- if (m_use)
- {
- m_camera.SetCameraMode(CameraMode.BLACK_WHILE); //usec
- }
- }
- }
- /// <summary>
- /// 彩色模式被点击
- /// </summary>
- private void caiseRadioButton_CheckedChanged(object sender, EventArgs e)
- {
- RadioButton rb = (RadioButton)sender;
- if (rb.Checked)
- {
- m_cameraParamModel.parame.Monochromatic = 0;
- // 设置到相机
- if (m_use)
- {
- m_camera.SetCameraMode(CameraMode.COLOR); //usec
- }
- }
- }
- private void zuobiaoXTextBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == (char)Keys.Enter)
- {
- int val = Convert.ToInt32(this.zuobiaoXTextBox.Text);
- if (val < 0)
- {
- this.zuobiaoXTextBox.Text = "0";
- }
- else if ((val + m_targetWidth) > m_resolution.Width)
- {
- zuobiaoXTextBox.Text = (m_resolution.Width - m_targetWidth).ToString();
- }
- TextChange();
- }
- if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- }
- }
- private void zuobiaoYTextBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == (char)Keys.Enter)
- {
- int val = Convert.ToInt32(this.zuobiaoYTextBox.Text);
- if (val < 0)
- {
- this.zuobiaoYTextBox.Text = "0";
- }
- else if ((val + m_targetHeight) > m_resolution.Height)
- {
- this.zuobiaoYTextBox.Text = (m_resolution.Height - m_targetHeight).ToString();
- }
- TextChange();
- }
- if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- }
- }
- private void chicunWTextBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == (char)Keys.Enter)
- {
- int val = Convert.ToInt32(this.chicunWTextBox.Text);
- if (val < 0)
- {
- this.chicunWTextBox.Text = "1";
- }
- else if ((val + Convert.ToInt32(this.zuobiaoXTextBox.Text)) > m_resolution.Width)
- {
- this.chicunWTextBox.Text = (m_resolution.Width - Convert.ToInt32(this.zuobiaoXTextBox.Text)).ToString();
- }
- TextChange();
- }
- if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- }
- }
- private void chicunHTextBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == (char)Keys.Enter)
- {
- int val = Convert.ToInt32(this.chicunHTextBox.Text);
- if (val < 0)
- {
- this.chicunHTextBox.Text = "1";
- }
- else if ((val + Convert.ToInt32(this.zuobiaoYTextBox.Text)) > m_resolution.Height)
- {
- this.chicunHTextBox.Text = (m_resolution.Height - Convert.ToInt32(this.zuobiaoYTextBox.Text)).ToString();
- }
- TextChange();
- }
- if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
- {
- e.Handled = true;
- }
- }
- private void TextChange()
- {
- m_targetX = Convert.ToInt32(this.zuobiaoXTextBox.Text);
- m_targetY = Convert.ToInt32(this.zuobiaoYTextBox.Text);
- m_targetWidth = Convert.ToInt32(this.chicunWTextBox.Text);
- m_targetHeight = Convert.ToInt32(this.chicunHTextBox.Text);
- SetROI();
- if (m_resolution.Width > 0 && m_resolution.Height > 0)
- {
- m_rect.X = m_targetX * this.pictureBox1.Width / m_resolution.Width;
- m_rect.Y = m_targetY * this.pictureBox1.Height / m_resolution.Height;
- m_rect.Width = m_targetWidth * this.pictureBox1.Width / m_resolution.Width;
- m_rect.Height = m_targetHeight * this.pictureBox1.Height / m_resolution.Height;
- }
- else
- {
- m_rect.X = m_targetX;
- m_rect.Y = m_targetY;
- m_rect.Width = m_targetWidth;
- m_rect.Height = m_targetHeight;
- }
-
- this.pictureBox1.Refresh();
- }
- private void SetROI()
- {
- if (m_use)
- {
- m_camera.SetROI(ref m_targetX, ref m_targetY, ref m_targetWidth, ref m_targetHeight);
- this.m_cameraParamModel.parame.previewRectX = m_targetX;
- this.m_cameraParamModel.parame.previewRectY = m_targetY;
- this.m_cameraParamModel.parame.previewRectW = m_targetWidth;
- this.m_cameraParamModel.parame.previewRectH = m_targetHeight;
- }
- }
- private void chicunHTextBox_Leave(object sender, EventArgs e)
- {
- int val = Convert.ToInt32(this.chicunHTextBox.Text);
- if (val < 0)
- {
- this.chicunHTextBox.Text = "1";
- }
- else if ((val + Convert.ToInt32(this.zuobiaoYTextBox.Text)) > m_resolution.Height)
- {
- this.chicunHTextBox.Text = (m_resolution.Height - Convert.ToInt32(this.zuobiaoYTextBox.Text)).ToString();
- }
- TextChange();
- }
- private void chicunWTextBox_Leave(object sender, EventArgs e)
- {
- int val = Convert.ToInt32(this.chicunWTextBox.Text);
- if (val < 0)
- {
- this.chicunWTextBox.Text = "1";
- }
- else if ((val + Convert.ToInt32(this.zuobiaoXTextBox.Text)) > m_resolution.Width)
- {
- this.chicunWTextBox.Text = (m_resolution.Width - Convert.ToInt32(this.zuobiaoXTextBox.Text)).ToString();
- }
- TextChange();
- }
- private void zuobiaoYTextBox_Leave(object sender, EventArgs e)
- {
- int val = Convert.ToInt32(this.zuobiaoYTextBox.Text);
- if (val < 0)
- {
- this.zuobiaoYTextBox.Text = "0";
- }
- else if ((val + m_targetHeight) > m_resolution.Height)
- {
- this.zuobiaoYTextBox.Text = (m_resolution.Height - m_targetHeight).ToString();
- }
- TextChange();
- }
- private void zuobiaoXTextBox_Leave(object sender, EventArgs e)
- {
- int val = Convert.ToInt32(this.zuobiaoXTextBox.Text);
- if (val < 0)
- {
- this.zuobiaoXTextBox.Text = "0";
- }
- else if ((val + m_targetWidth) > m_resolution.Width)
- {
- zuobiaoXTextBox.Text = (m_resolution.Width - m_targetWidth).ToString();
- }
- TextChange();
- }
- }
- }
|