| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994 | 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{    /// <summary>    /// 相机设置,边框界面    /// </summary>    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        /// <summary>        /// 相机参数的Model        /// </summary>        private CameraParamModel m_cameraParamModel;        /// <summary>        /// 相机设置的dialog        /// </summary>        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);        }        /// <summary>        /// 彩色模式被点击        /// </summary>        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;        }        /// <summary>        /// 设置下拉等数据源        /// </summary>        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<string> 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();            }        }        /// <summary>        /// 黑白模式被点击        /// </summary>         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();        }        /// <summary>        /// 刷新缩略图        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        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;            }        }        /// <summary>        /// 当发生图像旋转后旋转ROI区域显示        /// </summary>        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    }}
 |