C#实现在图片上绘图(填充)以及橡皮擦功能
使用的是winform
还是新手,用于记录学习过程,如何能够帮到他人就更好不过了。
大致采用的控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _08_ROI区域
{
public partial class Form1 : Form
{
public Graphics DrawTools_Graphics; //目标绘图板
private Pen p;
private int lineWidth = 3;//划线的宽度设置为3
private Image orginalImg;//原始画布,用来保存已经完成的绘图过程
private Color drawColor = Color.Black;//绘图的画笔颜色初始化为黑色
private Color backColor = Color.White;//绘图区域的背景颜色
private Graphics newgraphics;//中间画板
private Image finishImg;//中间画布,用来保存绘图过程中的痕迹
private SolidBrush br =null;//画刷
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "请选择图片";
ofd.Filter = "所有文件|*.*";
ofd.InitialDirectory = @"C:\Users\lh\Desktop\test\4444\Project\image";
ofd.Multiselect = false;
if (ofd.ShowDialog()==DialogResult.OK)
{
string image_path = ofd.FileName;
image = Image.FromFile(image_path);
bmp = new Bitmap(image);
image.Dispose();
//原始画布
orginalImg = (Bitmap)bmp.Clone();
//中间画布
finishImg = new Bitmap(bmp.Width, bmp.Height);
finishImg.Save("finish.bmp");
pictureBox1.Image = bmp;
//橡皮擦颜色
br = new SolidBrush(Color.Transparent);
Console.WriteLine(this.BackColor);
}
}
//图片路径
Image image;
Image bmp;
Point start; //起始点
Point end; //结束点
bool blnDraw; //在MouseMove事件中判断是否绘制矩形框
List<Point> polyPoints = null;//鼠标点击的坐标点
bool cliceMenu = true;//判断是否绘制多边形
string sign_type = "fill";
//绘制图层
Bitmap img;
//绘制结果图层
Bitmap sign_img;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
img = (Bitmap)orginalImg.Clone();//为了防止直接对原始画布进行改写
newgraphics = Graphics.FromImage(img);//实例化中间画板
newgraphics.Clear(Color.Transparent);
switch (sign_type)
{
case "fill":
{
//填充
switch (comboBox1.Text)
{
case "矩形":
//绘制矩形框 并填充
if (e.Button == MouseButtons.Left)
{
start = e.Location;
blnDraw = true;
}
break;
case "多边形":
{
Graphics g = pictureBox1.CreateGraphics();
//绘制多边形 low版本
if (cliceMenu) //首先第一次创建两个点如下(e.X, e.Y),( e.X + 1, e.Y + 1)
{
if (e.Button &#