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 System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Threading; using System.Drawing.Imaging; using PaintDotNet.Camera; namespace PaintDotNet.Setting.LabelComponent { /// /// 相机设置,边框界面 /// public class FrameCameraControl : UserControl { #region 控件 private GroupBox groupBox1; private ComboBox cameraComboBox; private RadioButton rbtColor; private RadioButton rbtGray; private GroupBox groupBox3; public PictureBox pictureBox1; private Button shuaxinButton; private Label label1; private TextBox txbX; private TextBox txbY; private Label label2; private TextBox txbH; private TextBox txbW; private Label label4; private Button btnFull; private Button btnCenter; private Label label3; #endregion /// /// 相机参数的Model /// private CameraParamModel m_cameraParamModel; /// /// 相机设置的dialog /// private Point m_startP; //画框的起始点 private bool m_blnDraw = false; private Rectangle m_rect = new Rectangle();//初始设置一个拖拽的显示框 SolidBrush m_brush = new SolidBrush(Color.Black); int m_handle = 0; Rectangle _lastRoiRect; Rectangle _nowRoiRect; private Size _resolution { get { var value = m_camera.Resolution; if ((_lastRotateId % 2) != 0) value = new Size(value.Height, value.Width); return value; } }// 分辨率 private ICamera m_camera; private bool m_use = false; // 设置是否立刻生效,设置到相机 string[] m_binArray = new string[] { "1x1 Bin", "2x2 Bin", "3x3 Bin", "4x4 Bin" }; private GroupBox groupBox2; private ComboBox cmbResolution; private Button btnLast; private Button btnSet; private System.Windows.Forms.Timer timer1; private IContainer components; private int m_selectedBin; public FrameCameraControl(CameraParamModel model, bool use, FrameCameraMode mode = FrameCameraMode.Color | FrameCameraMode.Resolution | FrameCameraMode.ROI) { this.m_use = use; this.m_cameraParamModel = model; this.m_camera = CameraManager.CurrentCamera; InitializeComponent(); InitializeLanguageText(); var height = 8; if ((mode & FrameCameraMode.Color) > 0) { height += groupBox1.Height; InitColorMode(); } else { groupBox1.Hide(); } if ((mode & FrameCameraMode.Resolution) > 0) { height += groupBox2.Height; InitResolution(); } else groupBox2.Hide(); if ((mode & FrameCameraMode.ROI) > 0) { height += groupBox3.Height; InitROI(); } else groupBox3.Hide(); Height = height; } protected override void Dispose(bool disposing = true) { timer1.Enabled = false; base.Dispose(disposing); } /// /// 彩色模式被点击 /// private void caiseRadioButton_CheckedChanged(object sender, EventArgs e) { RadioButton rb = (RadioButton)sender; if (rb == rbtColor && rb.Checked) { m_cameraParamModel.parame.Monochromatic = 0; // 设置到相机 if (m_use) { m_camera.GrayMode = 0; //usec } } } public void ResetCameraParamModel(CameraParamModel model) { m_cameraParamModel = model; InitColorMode(); InitResolution(); InitROI(); } private void InitializeLanguageText() { this.groupBox1.Text = PdnResources.GetString("Menu.Cameramode.text"); this.rbtColor.Text = PdnResources.GetString("Menu.Colormode.text"); this.rbtGray.Text = PdnResources.GetString("Menu.lx.text"); this.groupBox3.Text = PdnResources.GetString("Menu.Setting.Text"); this.btnFull.Text = PdnResources.GetString("Menu.Fullframe.text"); this.btnCenter.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"); this.groupBox2.Text = PdnResources.GetString("Menu.Resolution.text"); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.rbtColor = new System.Windows.Forms.RadioButton(); this.rbtGray = new System.Windows.Forms.RadioButton(); this.cameraComboBox = new System.Windows.Forms.ComboBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.btnSet = new System.Windows.Forms.Button(); this.btnLast = new System.Windows.Forms.Button(); this.label3 = new System.Windows.Forms.Label(); this.btnFull = new System.Windows.Forms.Button(); this.btnCenter = new System.Windows.Forms.Button(); this.txbH = new System.Windows.Forms.TextBox(); this.txbW = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.txbY = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.txbX = 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.groupBox2 = new System.Windows.Forms.GroupBox(); this.cmbResolution = new System.Windows.Forms.ComboBox(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.groupBox1.SuspendLayout(); this.groupBox3.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.groupBox2.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.rbtColor); this.groupBox1.Controls.Add(this.rbtGray); this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top; this.groupBox1.Location = new System.Drawing.Point(4, 4); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(480, 61); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "相机模式"; // // rbtColor // this.rbtColor.AutoSize = true; this.rbtColor.Checked = true; this.rbtColor.Location = new System.Drawing.Point(261, 25); this.rbtColor.Name = "rbtColor"; this.rbtColor.Size = new System.Drawing.Size(71, 16); this.rbtColor.TabIndex = 0; this.rbtColor.TabStop = true; this.rbtColor.Text = "彩色模式"; this.rbtColor.UseVisualStyleBackColor = true; this.rbtColor.CheckedChanged += new System.EventHandler(this.caiseRadioButton_CheckedChanged); // // rbtGray // this.rbtGray.AutoSize = true; this.rbtGray.Location = new System.Drawing.Point(19, 25); this.rbtGray.Name = "rbtGray"; this.rbtGray.Size = new System.Drawing.Size(71, 16); this.rbtGray.TabIndex = 1; this.rbtGray.TabStop = true; this.rbtGray.Text = "黑白模式"; this.rbtGray.UseVisualStyleBackColor = true; this.rbtGray.CheckedChanged += new System.EventHandler(this.heibaiRadioButton_CheckedChanged); // // cameraComboBox // this.cameraComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cameraComboBox.FormattingEnabled = true; this.cameraComboBox.Location = new System.Drawing.Point(19, 56); this.cameraComboBox.Name = "cameraComboBox"; this.cameraComboBox.Size = new System.Drawing.Size(416, 20); this.cameraComboBox.TabIndex = 2; this.cameraComboBox.SelectedIndexChanged += new System.EventHandler(this.cameraComboBox_SelectedIndexChanged); // // groupBox3 // this.groupBox3.Controls.Add(this.btnSet); this.groupBox3.Controls.Add(this.btnLast); this.groupBox3.Controls.Add(this.label3); this.groupBox3.Controls.Add(this.btnFull); this.groupBox3.Controls.Add(this.btnCenter); this.groupBox3.Controls.Add(this.txbH); this.groupBox3.Controls.Add(this.txbW); this.groupBox3.Controls.Add(this.label4); this.groupBox3.Controls.Add(this.txbY); this.groupBox3.Controls.Add(this.label2); this.groupBox3.Controls.Add(this.txbX); this.groupBox3.Controls.Add(this.label1); this.groupBox3.Controls.Add(this.shuaxinButton); this.groupBox3.Controls.Add(this.pictureBox1); this.groupBox3.Dock = System.Windows.Forms.DockStyle.Top; this.groupBox3.Location = new System.Drawing.Point(4, 159); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(480, 354); this.groupBox3.TabIndex = 2; this.groupBox3.TabStop = false; this.groupBox3.Text = "设置"; // // btnSet // this.btnSet.Location = new System.Drawing.Point(389, 324); this.btnSet.Name = "btnSet"; this.btnSet.Size = new System.Drawing.Size(75, 23); this.btnSet.TabIndex = 14; this.btnSet.Text = "刷新"; this.btnSet.UseVisualStyleBackColor = true; this.btnSet.Click += new System.EventHandler(this.btnSet_Click); // // btnLast // this.btnLast.Location = new System.Drawing.Point(389, 288); this.btnLast.Name = "btnLast"; this.btnLast.Size = new System.Drawing.Size(75, 23); this.btnLast.TabIndex = 13; this.btnLast.Text = "上次"; this.btnLast.UseVisualStyleBackColor = true; this.btnLast.Click += new System.EventHandler(this.btnLast_Click); // // 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 = "×"; // // btnFull // this.btnFull.Location = new System.Drawing.Point(298, 325); this.btnFull.Name = "btnFull"; this.btnFull.Size = new System.Drawing.Size(75, 23); this.btnFull.TabIndex = 11; this.btnFull.Text = "全幅"; this.btnFull.UseVisualStyleBackColor = true; this.btnFull.Click += new System.EventHandler(this.btnFull_Click); // // btnCenter // this.btnCenter.Location = new System.Drawing.Point(298, 288); this.btnCenter.Name = "btnCenter"; this.btnCenter.Size = new System.Drawing.Size(75, 23); this.btnCenter.TabIndex = 10; this.btnCenter.UseVisualStyleBackColor = true; this.btnCenter.Click += new System.EventHandler(this.btnCenter_Click); // // txbH // this.txbH.Location = new System.Drawing.Point(208, 325); this.txbH.Name = "txbH"; this.txbH.Size = new System.Drawing.Size(62, 21); this.txbH.TabIndex = 9; this.txbH.Text = "100"; this.txbH.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.zuobiaoXTextBox_KeyPress); // // txbW // this.txbW.Location = new System.Drawing.Point(99, 325); this.txbW.Name = "txbW"; this.txbW.Size = new System.Drawing.Size(62, 21); this.txbW.TabIndex = 7; this.txbW.Text = "100"; this.txbW.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.zuobiaoXTextBox_KeyPress); // // 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 = "图像尺寸:"; // // txbY // this.txbY.Location = new System.Drawing.Point(208, 291); this.txbY.Name = "txbY"; this.txbY.Size = new System.Drawing.Size(62, 21); this.txbY.TabIndex = 5; this.txbY.Text = "100"; this.txbY.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.zuobiaoXTextBox_KeyPress); // // 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 = "×"; // // txbX // this.txbX.Location = new System.Drawing.Point(99, 291); this.txbX.Name = "txbX"; this.txbX.Size = new System.Drawing.Size(62, 21); this.txbX.TabIndex = 3; this.txbX.Text = "100"; this.txbX.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.zuobiaoXTextBox_KeyPress); // // 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.StretchImage; 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.MouseMove += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseMove); this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.PictureBox1_MouseUp); // // groupBox2 // this.groupBox2.Controls.Add(this.cameraComboBox); this.groupBox2.Controls.Add(this.cmbResolution); this.groupBox2.Dock = System.Windows.Forms.DockStyle.Top; this.groupBox2.Location = new System.Drawing.Point(4, 65); this.groupBox2.Margin = new System.Windows.Forms.Padding(2); this.groupBox2.Name = "groupBox2"; this.groupBox2.Padding = new System.Windows.Forms.Padding(2); this.groupBox2.Size = new System.Drawing.Size(480, 94); this.groupBox2.TabIndex = 3; this.groupBox2.TabStop = false; this.groupBox2.Text = "分辨率"; // // cmbResolution // this.cmbResolution.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbResolution.FormattingEnabled = true; this.cmbResolution.Location = new System.Drawing.Point(19, 26); this.cmbResolution.Name = "cmbResolution"; this.cmbResolution.Size = new System.Drawing.Size(416, 20); this.cmbResolution.TabIndex = 0; this.cmbResolution.SelectedIndexChanged += new System.EventHandler(this.cmbResolution_SelectedIndexChanged); // // timer1 // this.timer1.Enabled = true; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // FrameCameraControl // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.Name = "FrameCameraControl"; this.Padding = new System.Windows.Forms.Padding(4); this.Size = new System.Drawing.Size(488, 520); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.groupBox3.ResumeLayout(false); this.groupBox3.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.groupBox2.ResumeLayout(false); this.ResumeLayout(false); } private void InitColorMode() { this.rbtGray.Checked = m_cameraParamModel.parame.Monochromatic == 1; this.rbtColor.Checked = m_cameraParamModel.parame.Monochromatic != 1; } /// /// 设置下拉等数据源 /// private void InitResolution() { this.cameraComboBox.Items.AddRange(m_binArray); int index = m_cameraParamModel.parame.BinningSumation - 1; if (index > m_binArray.Length || index < 0) { index = 0; } this.cameraComboBox.SelectedIndex = index; cmbResolution.Items.Clear(); List resolutions = this.m_camera.GetResolutionList(); for (int i = 0; i < resolutions.Count; i++) { cmbResolution.Items.Add(resolutions[i]); } if (resolutions.Count > 0) { cmbResolution.SelectedIndex = m_cameraParamModel.parame.Resolution; } } private void InitROI() { ROIReset(); } private void cameraComboBox_SelectedIndexChanged(object sender, EventArgs e) { m_selectedBin = cameraComboBox.SelectedIndex + 1; if (m_selectedBin == m_cameraParamModel.parame.BinningSumation) { return; } m_cameraParamModel.parame.BinningSumation = m_selectedBin; } private void cmbResolution_SelectedIndexChanged(object sender, EventArgs e) { m_cameraParamModel.parame.Resolution = cmbResolution.SelectedIndex; int resolutionId = m_camera.ResolutionId; if (resolutionId != m_cameraParamModel.parame.Resolution) { // 分辨率 m_camera.ResolutionId = m_cameraParamModel.parame.Resolution; ROIReset(); } } /// /// 黑白模式被点击 /// private void heibaiRadioButton_CheckedChanged(object sender, EventArgs e) { RadioButton rb = (RadioButton)sender; if (rb == rbtGray && rbtGray.Checked) { m_cameraParamModel.parame.Monochromatic = 1; // 设置到相机 if (m_use) { m_camera.GrayMode = 1; } } } #region Oprate Buttons //中心按钮被点击 private void btnCenter_Click(object sender, EventArgs e) { if (btnCenter.Text.Equals(PdnResources.GetString("Menu.center.text"))) { btnCenter.Text = PdnResources.GetString("Menu.Start.text"); _nowRoiRect.X = (_resolution.Width - _nowRoiRect.Width) / 2; _nowRoiRect.Y = (_resolution.Height - _nowRoiRect.Height) / 2; } else { btnCenter.Text = PdnResources.GetString("Menu.center.text"); _nowRoiRect.X = 0; _nowRoiRect.Y = 0; } SetROI(); } //全幅 private void btnFull_Click(object sender, EventArgs e) { _lastRoiRect = _nowRoiRect; _nowRoiRect = new Rectangle(0, 0, _resolution.Width, _resolution.Height); SetROI(); } private void btnLast_Click(object sender, EventArgs e) { _nowRoiRect = _lastRoiRect; _lastRoiRect = Rectangle.Empty; SetROI(); } /// /// 刷新缩略图 /// /// /// private void button1_Click(object sender, EventArgs e) { ShootAndShow(); } private void ShootAndShow() { CameraManager.Shoot((bitmap) => { if (bitmap == null) return; if (bitmap.PixelFormat == PixelFormat.Format8bppIndexed) { ColorPalette palette = bitmap.Palette; for (int i = 0; i < 256; i++) { palette.Entries[i] = Color.FromArgb(i, i, i); } bitmap.Palette = palette; } this.pictureBox1.Image = bitmap; }); } #endregion #region picture event private void PictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) return; 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; m_blnDraw = true; } private void PictureBox1_MouseMove(object sender, MouseEventArgs e) { if (m_handle > 0 && m_blnDraw) { MoveHandleTo(e.Location, m_handle); UpdateRoiText(); } } private void PictureBox1_MouseUp(object sender, MouseEventArgs e) { m_blnDraw = false; //结束绘制 this.Cursor = Cursors.Default; SetROI(); } private void imageBox1_Paint(object sender, PaintEventArgs e) { if (_resolution.Width != 0 && _resolution.Height != 0) { if (!m_blnDraw) { double _scarHor = pictureBox1.Width * 1.0 / _resolution.Width; double _scarVal = pictureBox1.Height * 1.0 / _resolution.Height; m_rect.X = (int)(_nowRoiRect.X * _scarHor); m_rect.Y = (int)(_nowRoiRect.Y * _scarVal); m_rect.Width = (int)(_nowRoiRect.Width * _scarHor); m_rect.Height = (int)(_nowRoiRect.Height * _scarVal); } 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)); } } } } #endregion #region handle 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) { point.X = point.X > pictureBox1.Width ? pictureBox1.Width : point.X; point.X = point.X < 0 ? 0 : point.X; point.Y = point.Y > pictureBox1.Height ? pictureBox1.Height : point.Y; point.Y = point.Y < 0 ? 0 : point.Y; 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; _nowRoiRect.X = m_rect.X * _resolution.Width / pictureBox1.Width; _nowRoiRect.Y = m_rect.Y * _resolution.Height / pictureBox1.Height; _nowRoiRect.Width = m_rect.Width * _resolution.Width / pictureBox1.Width; _nowRoiRect.Height = m_rect.Height * _resolution.Height / pictureBox1.Height; pictureBox1.Refresh(); m_startP = point; } #endregion private void zuobiaoXTextBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Enter) { var textBox = sender as Control; int val = Convert.ToInt32(textBox.Text); TextChange(); } if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) { e.Handled = true; } } private void TextChange() { var x = Convert.ToInt32(this.txbX.Text); var y = Convert.ToInt32(this.txbY.Text); var w = Convert.ToInt32(this.txbW.Text); var h = Convert.ToInt32(this.txbH.Text); _nowRoiRect = new Rectangle(x, y, w, h); SetROI(); } private void SetROI() { if (m_use) { var w = _nowRoiRect.Width; var h = _nowRoiRect.Height; var x = _nowRoiRect.X; var y = _resolution.Height - _nowRoiRect.Y - h; m_camera.SetROI(x, y, w, h); m_cameraParamModel.SetPreviewRect(_nowRoiRect); } this.pictureBox1.Refresh(); UpdateRoiText(); } private void ROIReset() { _lastRoiRect = Rectangle.Empty; int x = 0, y = 0, w = _resolution.Width, h = _resolution.Height; _nowRoiRect = new Rectangle(x, y, w, h); ShootAndShow(); SetROI(); } private void timer1_Tick(object sender, EventArgs e) { this.Enabled = Camera.CameraManager.IsLive; btnLast.Enabled = _lastRoiRect != Rectangle.Empty; btnFull.Enabled = _nowRoiRect.Size != _resolution; btnFull.Enabled = _nowRoiRect.Size != _resolution; btnCenter.Enabled = _nowRoiRect.Size != _resolution; btnSet.Enabled = _nowRoiRect != GetTextRect() && !m_blnDraw; Transfor(); } private void UpdateRoiText() { txbX.Text = _nowRoiRect.X.ToString(); txbY.Text = _nowRoiRect.Y.ToString(); txbW.Text = _nowRoiRect.Width.ToString(); txbH.Text = _nowRoiRect.Height.ToString(); } private Rectangle GetTextRect() { try { var x = Convert.ToInt32(this.txbX.Text); var y = Convert.ToInt32(this.txbY.Text); var w = Convert.ToInt32(this.txbW.Text); var h = Convert.ToInt32(this.txbH.Text); return new Rectangle(x, y, w, h); } catch { return new Rectangle(new Point(0, 0), _resolution); } } private void btnSet_Click(object sender, EventArgs e) { TextChange(); } #region Transformation int _lastRotateId = 0; private CameraParamModel.ParameterSets _settings { get { return CameraConfigs.Settings; } } /// /// 当发生图像旋转后旋转ROI区域显示 /// public void Transfor() { if (_lastRotateId == 0 && _settings.Rotate == 1 || _lastRotateId == 3 && _settings.Rotate == 0) { R90(); this.pictureBox1.Refresh(); } if (_lastRotateId == 0 && _settings.Rotate == 3 || _lastRotateId == 1 && _settings.Rotate == 0) { L90(); this.pictureBox1.Refresh(); } if (_lastRotateId == 0 && _settings.Rotate == 2 || _lastRotateId == 1 && _settings.Rotate == 3) R180(); _lastRotateId = _settings.Rotate; UpdateRoiText(); this.pictureBox1.Refresh(); } void R90() { var newRect = new Rectangle(); newRect.Width = _nowRoiRect.Height; newRect.Height = _nowRoiRect.Width; newRect.Y = _nowRoiRect.X; newRect.X = _resolution.Height - _nowRoiRect.Y - _nowRoiRect.Height; _nowRoiRect = newRect; } void L90() { var newRect = new Rectangle(); newRect.Width = _nowRoiRect.Height; newRect.Height = _nowRoiRect.Width; newRect.Y = _resolution.Width - _nowRoiRect.X - _nowRoiRect.Width; newRect.X = _nowRoiRect.Y; _nowRoiRect = newRect; } void R180() { var newRect = new Rectangle(); newRect.Width = _nowRoiRect.Width; newRect.Height = _nowRoiRect.Height; newRect.X = _resolution.Width - _nowRoiRect.X - _nowRoiRect.Width; newRect.Y = _resolution.Height - _nowRoiRect.Y - _nowRoiRect.Height; _nowRoiRect = newRect; } #endregion } public enum FrameCameraMode { Color = 1, Resolution = 2, ROI = 4 } }