在winform里面,没有默认的任意图形填充函数,但opencv里面有,结合opencv可以轻松实现图形填充。代码如下:
引入库
using OpenCvSharp;
using OpenCvSharp.Extensions;
1、创建类数据成员bitmap
Bitmap bitmap;
2、在初始化后为pictureBox1的image和bitmap初始化(加上1,代码如下)
itmap bitmap;
public Form1()
{
InitializeComponent();
bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = bitmap;
}
3、在pictureBox上面对image进行绘图
var g = Graphics.FromImage(pictureBox1.Image) ;
Pen p = new Pen(Color.Red,3);
var rect = new Rectangle(0,0, 400,400);
var rect1 = new Rectangle(50, 50, 300, 300);
g.DrawEllipse(p, rect);
g.DrawEllipse(p, rect1);
g.Dispose();
pictureBox1.Refresh();
4、填充
bitmap = (Bitmap)pictureBox1.Image;
Mat img1= BitmapConverter.ToMat(bitmap);
Cv2.CvtColor(img1, img1, ColorConversionCodes.BGRA2BGR);
var pt1 = new OpenCvSharp.Point(150, 25);
Cv2.FloodFill(img1, pt1, Scalar.Red);
bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(img1);
pictureBox1.Image = bitmap;
pictureBox1.Refresh();
显示结果如下:
?
|