在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C#鼠标改变ROI窗口大小

C#鼠标改变ROI窗口大小

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:16.24M
  • 下载次数:75
  • 浏览次数:436
  • 发布时间:2021-03-27
  • 实例类别:C#语言基础
  • 发 布 人:yy11101244
  • 文件格式:.7z
  • 所需积分:1
 相关标签: ROI 交互

实例介绍

【实例简介】C#鼠标改变ROI窗口大小

【实例截图】

from clipboard

【核心代码】

/// <summary>
        /// 改变鼠标样式
        /// </summary>
        private void ChangeCursor(Point loc, bool DrawOrMove)
        {
            if (DrawOrMove)
            {
                if (m_OperateRectangle[0].Contains(loc) || m_OperateRectangle[7].Contains(loc))
                    this.Cursor = Cursors.SizeNWSE;
                else if (m_OperateRectangle[1].Contains(loc) || m_OperateRectangle[6].Contains(loc))
                    this.Cursor = Cursors.SizeNS;
                else if (m_OperateRectangle[2].Contains(loc) || m_OperateRectangle[5].Contains(loc))
                    this.Cursor = Cursors.SizeNESW;
                else if (m_OperateRectangle[3].Contains(loc) || m_OperateRectangle[4].Contains(loc))
                    this.Cursor = Cursors.SizeWE;
                else if (CaptureRectangle.Contains(loc))
                    this.Cursor = Cursors.SizeAll;
                else
                    this.Cursor = Cursors.Default;
            }

        }

        /// <summary>
        /// MouseState=1:当鼠标双击ROI之外的图像区域时候,保留已存在ROI区域;
        /// MouseState=2:根据鼠标在8个矩形框位置来确定是改变宽度还是高度;
        /// </summary>
        private void KeepMouseState(int MouseState, MouseEventArgs e)
        {

            if (m_OperateRectangle[0].Contains(e.Location))
            {
                MouseMovePoint.X = this.CaptureRectangle.Right;
                MouseMovePoint.Y = this.CaptureRectangle.Bottom;
            }
            else if (m_OperateRectangle[1].Contains(e.Location))
            {
                MouseMovePoint.Y = this.CaptureRectangle.Bottom;
                BlockWidth       = true;//锁定ROI宽度

            }
            else if (m_OperateRectangle[2].Contains(e.Location))
            {
                MouseMovePoint.X = this.CaptureRectangle.X;
                MouseMovePoint.Y = this.CaptureRectangle.Bottom;
            }
            else if (m_OperateRectangle[3].Contains(e.Location))
            {
                MouseMovePoint.X = this.CaptureRectangle.Right;
                BlockHeight      = true;//锁定ROI高度
            }
            else if (m_OperateRectangle[4].Contains(e.Location))
            {
                MouseMovePoint.X = this.CaptureRectangle.X;
                BlockHeight      = true;//锁定ROI高度
            }
            else if (m_OperateRectangle[5].Contains(e.Location))
            {
                MouseMovePoint.X = this.CaptureRectangle.Right;
                MouseMovePoint.Y = this.CaptureRectangle.Y;
            }
            else if (m_OperateRectangle[6].Contains(e.Location))
            {
                MouseMovePoint.Y = this.CaptureRectangle.Y;
                BlockWidth       = true;//锁定ROI宽度

            }
            else if (m_OperateRectangle[7].Contains(e.Location))
            {
                MouseMovePoint = this.CaptureRectangle.Location;

            }else if(this.CaptureRectangle.Contains(e.Location))
            {

            }
            else
            {
                if (MouseState == 1)//当在ROI矩形外按下鼠标,保留现有的ROI,防止ROI被刷掉
                {
                    StartDraw = false;
                }

            }

        }


        /// <summary>
        /// 鼠标按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                StartDraw   = true;
                BlockWidth  = false;
                BlockHeight = false;
                KeepMouseState(1,e);
                MouseDownPoint = new Point(e.X, e.Y);
                //通过鼠标移动坐标位置来判断是改变ROI大小还是移动ROI操作
                if (FinishedDraw)
                {
                    if (this.CaptureRectangle.Contains(e.Location))
                    {
                        IsMoving = true;
                    }
                    else
                    {
                        IsMoving = false;
                    }
                }
            }
        }


        /// <summary>
        /// 鼠标弹起事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseUp(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                StartDraw       = false;
                TempStart_Point = CaptureRectangle.Location;//记录下鼠标弹起时,ROI当前位置
                CutImage();
            }
        }


        /// <summary>
        /// 鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMouseMove(object sender, MouseEventArgs e)
        {

            ChangeCursor(e.Location, true);

            if (StartDraw)
            {

                    KeepMouseState(2, e);
            }



            if (StartDraw)
            {
                if (IsMoving)
                {
                    this.CaptureRectangle.X = TempStart_Point.X e.X - MouseDownPoint.X;
                    this.CaptureRectangle.Y = TempStart_Point.Y e.Y - MouseDownPoint.Y;
                    if (this.CaptureRectangle.X < 0) this.CaptureRectangle.X = 0;
                    if (this.CaptureRectangle.Y < 0) this.CaptureRectangle.Y = 0;
                    if (this.CaptureRectangle.Right > OriginImage.Width) this.CaptureRectangle.X = OriginImage.Width - this.CaptureRectangle.Width - 1;
                    if (this.CaptureRectangle.Bottom > OriginImage.Height) this.CaptureRectangle.Y = OriginImage.Height - this.CaptureRectangle.Height - 1;

                }
                else
                {

                    if (Math.Abs(e.X - MouseMovePoint.X) > 1 || Math.Abs(e.Y - MouseMovePoint.Y) > 1)
                    {
                        if ((e.X >= 0 && e.X <= this.OriginImage.Width) && (e.Y >= 0 && e.Y <= this.OriginImage.Height))
                        {
                            //当前坐标在图像区域
                        }
                        else
                        {
                            //当前坐标不在图像区域
                            return;
                        }

                        if (!BlockWidth)
                        {
                            CaptureRectangle.X = MouseMovePoint.X - e.X < 0 ? MouseMovePoint.X : e.X;//以CaptureRectangle的Left或Right为正负分界线,MouseMovePoint.X - e.X < 0:正向方向改变大小,MouseMovePoint.X - e.X > 0:负向方向改变大小
                            CaptureRectangle.Width = Math.Abs(MouseMovePoint.X - e.X);
                        }


                        if (!BlockHeight)
                        {
                            CaptureRectangle.Y = MouseMovePoint.Y - e.Y < 0 ? MouseMovePoint.Y : e.Y;//以CaptureRectangle的Top或Bttom为正负分界线,MouseMovePoint.Y - e.Y < 0:正向方向改变大小,MouseMovePoint.Y - e.Y > 0:负向方向改变大小
                            CaptureRectangle.Height = Math.Abs(MouseMovePoint.Y - e.Y);
                        }


                    }

                }

                //使画布无效,从而执行OnPaint()
                this.DisplayImage_pictureBox.Invalidate();
               
            }


        }


        /// <summary>
        /// 控件Paint事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnPaint(object sender, PaintEventArgs e)
        {
            if (FinishedDraw)
            {
                Pen p = new Pen(Color.YellowGreen, 1);
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                e.Graphics.DrawRectangle(p, CaptureRectangle);
                //绘制8个小矩形框
                DrawOperationBox(e.Graphics);
            }

        }


        /// <summary>
        /// 绘制8个小矩形框
        /// </summary>
        /// <param name="g"></param>
        protected virtual void DrawOperationBox(Graphics g)
        {

            m_OperateRectangle[0].X = this.CaptureRectangle.X - 5;
            m_OperateRectangle[0].Y =  this.CaptureRectangle.Y - 5;
            m_OperateRectangle[1].X = this.CaptureRectangle.X this.CaptureRectangle.Width / 2 - 5;
            m_OperateRectangle[1].Y = m_OperateRectangle[2].Y = this.CaptureRectangle.Y - 7;
            m_OperateRectangle[2].X = this.CaptureRectangle.Right - 5;
            m_OperateRectangle[2].Y = this.CaptureRectangle.Y - 5;
            m_OperateRectangle[3].X = this.CaptureRectangle.X - 7;
            m_OperateRectangle[3].Y = this.CaptureRectangle.Y this.CaptureRectangle.Height / 2 - 5;
            m_OperateRectangle[4].X = this.CaptureRectangle.Right - 2;
            m_OperateRectangle[4].Y = this.CaptureRectangle.Y this.CaptureRectangle.Height / 2 - 5;
            m_OperateRectangle[5].X = this.CaptureRectangle.X - 5;
            m_OperateRectangle[5].Y = this.CaptureRectangle.Bottom - 5;
            m_OperateRectangle[6].X = this.CaptureRectangle.X this.CaptureRectangle.Width / 2 - 5;
            m_OperateRectangle[6].Y = this.CaptureRectangle.Bottom - 2;
            m_OperateRectangle[7].X = this.CaptureRectangle.Right - 5;
            m_OperateRectangle[7].Y = this.CaptureRectangle.Bottom - 5;

            if (this.CaptureRectangle.Width > 10 && this.CaptureRectangle.Height > 10)
            {
                SolidBrushObject.Color = Color.YellowGreen;
                foreach (Rectangle rect in m_OperateRectangle)
                {
                    g.FillRectangle(SolidBrushObject, rect);
                }
            }

        }


        /// <summary>
        /// 画矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDrawRectangle(object sender, EventArgs e)
        {
            CaptureRectangle.X      = OriginImage.Width/2;
            CaptureRectangle.Width  = OriginImage.Width / 4;


            CaptureRectangle.Y      = OriginImage.Height / 2;
            CaptureRectangle.Height = OriginImage.Height / 4;

            TempStart_Point         = CaptureRectangle.Location;
            FinishedDraw            = true;
            IsMoving                = false;
            this.DisplayImage_pictureBox.Invalidate();
        }


        /// <summary>
        /// 剪切图像
        /// </summary>
        private Bitmap  CutImage()
        {
            Bitmap   CutBitmap      = new Bitmap(CaptureRectangle.Width, CaptureRectangle.Height);

            Graphics GraphicsObject = Graphics.FromImage(CutBitmap);

            GraphicsObject.DrawImage(OriginImage, new Rectangle(0, 0, CaptureRectangle.Width, CaptureRectangle.Height), CaptureRectangle, GraphicsUnit.Pixel);

            this.CutImage_pictureBox.Image  = CutBitmap;

            this.CutImage_pictureBox.Width  = CutBitmap.Width;

            this.CutImage_pictureBox.Height = CutBitmap.Height;

            GraphicsObject.Dispose();

            return CutBitmap;
        }

标签: ROI 交互

实例下载地址

C#鼠标改变ROI窗口大小

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警