C#自学39一在picturebox控件上画ROI区域

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace Auto_Detection_System
{
    public partial class FormDrawROI : Form
    {
        public FormDrawROI()
        {
            InitializeComponent();
        }

        private System.Drawing.Point rectStartPoint, rectEndPoint;
        public bool b_CreateModel = false;
        bool b_DrawRoi;//判断Roi区域绘制
        Rect roiRect;

        //坐标转换  //解决拉伸问题
        private static void ConvertCoordinates(PictureBox pic, out int X0, out int Y0, int x, int y)
        {
            int pic_hgt = pic.ClientSize.Height;
            int pic_wid = pic.ClientSize.Width;
            int img_hgt = pic.Image.Height;
            int img_wid = pic.Image.Width;

            X0 = x;
            Y0 = y;
            switch (pic.SizeMode)
            {
                case PictureBoxSizeMode.AutoSize:
                case PictureBoxSizeMode.StretchImage:
                    X0 = (int)(img_wid * x / (float)pic_wid);
                    Y0 = (int)(img_hgt * y / (float)pic_hgt);
                    break;
            }
        }
        private void picBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (true)
            {
                if (e.Button == MouseButtons.Left)
                {
                    rectStartPoint = e.Location; //获得鼠标按下的pictureBox上坐标
                    Invalidate();
                    b_DrawRoi = true;//判断标志
                }
                else if (e.Button == MouseButtons.Right)
                {
                    //roiImage = new Mat(srcImage, roiRect);
                    //templateImgContours = ImageTemplateContours(roiImage);

                    b_DrawRoi = false; //结束绘制
                    rectStartPoint = rectEndPoint;
                    pictureBox2.Refresh();//消掉画的框
                }
            }
        }

        private void picBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (b_DrawRoi)
            {
                if (e.Button != MouseButtons.Left)//判断是否按下左键
                {
                    return;
                }

                rectEndPoint = e.Location; //记录框的位置和大小

                pictureBox2.Invalidate();

                // 最后点位置
                int X0, Y0;
                ConvertCoordinates(pictureBox2, out X0, out Y0, e.X, e.Y);

                //Create ROI 感兴趣区域
                ConvertCoordinates(pictureBox2, out X0, out Y0, rectStartPoint.X, rectStartPoint.Y);
                int X1, Y1;
                ConvertCoordinates(pictureBox2, out X1, out Y1, rectEndPoint.X, rectEndPoint.Y);

                roiRect = new Rect(Math.Min(X0, X1), Math.Min(Y0, Y1), Math.Abs(X0 - X1), Math.Abs(Y0 - Y1));
            }
        }

        private void picBox_Paint(object sender, PaintEventArgs e)
        {
            if (rectStartPoint.Equals(rectEndPoint)) return;
            e.Graphics.DrawRectangle(System.Drawing.Pens.Red, rectStartPoint.X, rectStartPoint.Y, rectEndPoint.X - rectStartPoint.X, rectEndPoint.Y - rectStartPoint.Y);
        }
    }
}

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值