美文网首页
C#:在画布内移动动态绘制矩形

C#:在画布内移动动态绘制矩形

作者: 大龙10 | 来源:发表于2022-09-19 16:30 被阅读0次

    《学习OpenCV(中文版)》

    作者:(美)布拉德斯基(Bradski,G.)
    (美)克勒(Kaehler,A.) 著
    出版社:清华大学出版社
    出版时间:2009年10月

    http://cn.voidcc.com/question/p-pneulhvn-qa.html
    

    一、MouseUp事件

    • 当鼠标移动时,鼠标指针所走过的每个像素都不会得到MouseMove事件。你会以相当一致的时间间隔得到它们,所以鼠标移动的速度越快,得到的点就越远。这个你不能做太多的细节。

    • MouseUp事件中取、存储最后一个点的位置,并在最后一个点和最后一个点之间的每个点绘制一个矩形。

    二、在画布内移动动态绘制矩形

    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.Forms;
    using OpenCvSharp;
    using OpenCvSharp.Extensions;
    
    
    namespace Ky_CvRoi
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public string SourceFname = @"d:\2022视觉文件夹\安柏.JPG";
            public Mat S0 = Cv2.ImRead(@"d:\2022视觉文件夹\安柏.JPG", ImreadModes.Unchanged);
            public OpenCvSharp.Point p1 = new OpenCvSharp.Point(-1, -1);
            public OpenCvSharp.Point p2 = new OpenCvSharp.Point(-1, -1);
            bool mouseDown = false; 
    
            #region  定义一个发送接收图像数据的全局变量SendPicData
            public Mat SendPicData  
            {
                get { return BitmapConverter.ToMat((Bitmap)pictureBox1.Image); }
                set { pictureBox1.Image = value.ToBitmap(); }
            }
            #endregion
    
    
            private void Form1_Load(object sender, EventArgs e)
            {
                pictureBox1.Image = S0.ToBitmap();
            }
    
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                mouseDown = true;
                if (mouseDown)
                {
                    p1.X = e.X;
                    p1.Y = e.Y;
                    textBox1.Text = textBox1.Text + "p1=(" + e.X.ToString() + " , " + e.Y.ToString() + ")\r\n";
                }
            }
    
    
            private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                mouseDown = false;
                    int w1;
                    int h1;
                    if ((p1.X >= 0) && (p1.Y >= 0))
                    {
                        p2.X = e.X;
                        p2.Y = e.Y;
                        textBox1.Text = textBox1.Text + "p2=(" + e.X.ToString() + " , " + e.Y.ToString() + ")\r\n";
                        w1 = p2.X - p1.X;
                        h1 = p2.Y - p1.Y;
                        if (p2.X < p1.X)
                        {
                            w1 = p1.X - p2.X;
                        };
                        if (p2.Y < p1.Y)
                        {
                            h1 = p1.Y - p2.Y;
                        }
                    }
    
                    OpenCvSharp.Scalar scalar = new OpenCvSharp.Scalar(0, 200, 200);
                    S0.Rectangle(p1, p2, scalar,2);
                    pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(S0);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                string path = @"d:\2022视觉文件夹\安柏.JPG";
                pictureBox1.Image = new Bitmap(path);
                S0 = new Mat(path);
                pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(S0);
            }
        }
    }
    
    

    三、运行结果

    相关文章

      网友评论

          本文标题:C#:在画布内移动动态绘制矩形

          本文链接:https://www.haomeiwen.com/subject/auuportx.html