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
{
///
/// 相机设置,边框界面
///
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
///
/// 相机参数的Model
///
private CameraParamModel m_cameraParamModel;
///
/// 相机设置的dialog
///
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();
}
///
/// 设置下拉等数据源
///
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();
}
///
/// 绑定样式数据
///
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;
}
///
/// 刷新缩略图
///
///
///
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;
}
}
///
/// 绘制
///
///
///
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;
}
}
///
/// 黑白模式被点击
///
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
}
}
}
///
/// 彩色模式被点击
///
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();
}
}
}