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);
        }
    }
}

你可能感兴趣的:(C#零基础自学,c#)