C#实现在图片上绘图(填充)以及橡皮擦功能

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 &#
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值