Emgu.CV C# WinFrom 鼠标画图
在窗口中添加picturebox1,在picturebox1中画图
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
namespace EmguCV
{
public partial class Form1 : Form
{
bool mousedown = false;
int pre_x = 0, pre_y = 0;
int x = 0, y = 0;
Image<Bgr, byte> a;
public Form1()
{
InitializeComponent();
a = new Image<Bgr, byte>(800, 800, new Bgr(255, 255, 255));
pictureBox1.Image = a.Bitmap;
}
private void button1_Click(object sender, EventArgs e)
{
//Mat img;
//img = CvInvoke.Imread("test.jpg", ImreadModes.Color);
//CvInvoke.Imshow("test", img);
try
{
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mousedown = true;
pre_x = e.X;
pre_y = e.Y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (mousedown == true)
{
x = e.X;
y = e.Y;
CvInvoke.Line(a, new Point(pre_x, pre_y), new Point(x, y), new MCvScalar(255, 0, 0), 2);
pre_x = x;
pre_y = y;
pictureBox1.Image = a.Bitmap;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
mousedown = false;
}
}
}