WINFORM画笔实现画板(如何实现橡皮擦和清空画板功能)

C#内部并没有提供橡皮擦功能所以,只能使用画笔和颜色填充来实现橡皮擦和清空画板功能。

此次小编写了一个简易的画板功能其中包含橡皮擦,清空面板,在窗体运行中修改画笔颜色和像素等功能。

代码如下:

using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.LinkLabel;

namespace 画板完整版
{
    public partial class Form1 : UIForm
    {
        // 定义绘制对象
        Graphics g;
        Bitmap bmp;

        // 起点坐标
        Point start; bool flag = false;

        // 画笔颜色
        Color c1 = Color.Black;

        //画笔粗细
        int size = 1;
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile("./1.png");
            bmp = (Bitmap)pictureBox1.Image.Clone();
            g = Graphics.FromImage(bmp);


            // 默认主题
            this.uiComboBox1.SelectedIndex = 0;
            this.uiStyleManager1.Style = (UIStyle)Enum.Parse(typeof(UIStyle), this.uiComboBox1.SelectedItem.ToString());

            // 默认画笔像素
            this.uiComboBox2.SelectedIndex = 0;

            // 设置XY轴默认像素
            this.uiLabel1.Text = "像素: { X:" + "0" + ",Y:" + "0 }";

            // 时间
            timer1.Interval = 1000;
            timer1.Enabled = true;
            this.time.Text = "北京时间: " + (DateTime.Now).Year + "年" + (DateTime.Now).Month + "月" + (DateTime.Now).Day + "日" + (DateTime.Now).Hour + "时" + (DateTime.Now).Minute + "分" + (DateTime.Now).Second + "秒";

        }

        // 时间
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.time.Text = "北京时间: " + (DateTime.Now).Year + "年" + (DateTime.Now).Month + "月" + (DateTime.Now).Day + "日" + (DateTime.Now).Hour + "时" + (DateTime.Now).Minute + "分" + (DateTime.Now).Second + "秒";
        }

        // 修改主题
        private void uiComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.uiStyleManager1.Style = (UIStyle)Enum.Parse(typeof(UIStyle), this.uiComboBox1.SelectedItem.ToString());
        }

        // 清空画板
        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            Point[] p =
            {
                new Point(-2,-2),
                new Point(810,-2),
                new Point(810,515),
                new Point(-2,515),
                new Point(-2,-2)
            };
            g.FillPolygon(Brushes.White, p);
            pictureBox1.Image = bmp;
        }

        // 设置画笔
        private void uiSymbolButton3_Click(object sender, EventArgs e)
        {
            if(s==0&&co==null)return;
            size = s; c1 = co;
            this.uiComboBox2.Text = size.ToString();
        }
        // 画笔颜色
        private void uiButton1_Click(object sender, EventArgs e)
        {
            DialogResult r = colorDialog1.ShowDialog();
            if (r == DialogResult.OK)
            {
                c1 = colorDialog1.Color;
            }
        }
        // 画笔粗细
        private void uiComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            size = int.Parse(uiComboBox2.SelectedItem.ToString());
        }

        // 鼠标按下的方法,主要是获取按下时候的坐标也就是起点坐标
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)// 点击了鼠标左键
            {
                start = e.Location; // 当前点击的坐标
                flag = true;
            }
        }
        // 鼠标移动的时候获取点,获取会追的结束点,并且划线
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            this.uiLabel1.Text = "像素: {X:" + e.X + ",Y:" +e.Y+"}";
            if (flag == false) return;
            g.DrawLine(new Pen(c1, size), start, e.Location);
            start = e.Location;// 重置起始点
            pictureBox1.Image = bmp;
        }
        // 结束绘制
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            flag = false;
            pictureBox1.Image = bmp;
        }
        // 橡皮
        int s = 0;
        Color co;
        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            s = size; co = c1;
            size = 20;
            c1 = Color.White;
            this.uiComboBox2.Text = size.ToString();
        }

        // 保存
        private void uiSymbolButton4_Click(object sender, EventArgs e)
        {
            if(bmp == null) return;
            SaveFileDialog sf = new SaveFileDialog(); // 保存对话框
            sf.Filter = "图像文件|*.png|jpg图片|*.jpg";
            if (sf.ShowDialog() == DialogResult.OK)
            {
                bmp.Save(sf.FileName, ImageFormat.Png); //
                MessageBox.Show("保存至" + sf.FileName, "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinForm是一种用于开发Windows桌面应用程序的技术,而仿Windows画板绘图指的是基于WinForm技术开发的一个应用程序,功能类似于Windows自带的画图工具。 WinForm实现的仿Windows画板绘图需要通过以下几个步骤完成: 1. 创建WinForm窗体应用程序:使用Visual Studio等集成开发环境创建一个新的WinForm项目。 2. 设计窗体界面:在窗体上添加各种控件,如菜单栏、工具栏、画板等,以及相应的事件处理函数。 3. 实现画图功能:在画板控件上实现绘图功能,可以使用GDI+进行绘图操作。比如,可以使用鼠标事件来捕捉用户的画图动作,然后根据用户的操作在画板上绘制相应的图形。 4. 添加工具栏与菜单栏:通过在窗体上添加工具栏和菜单栏,为用户提供各种绘图工具和选项,如画笔橡皮擦、线条颜色、线条粗细等。 5. 实现保存与打开功能:为了让用户能够保存绘制的图形,应该提供保存和打开功能。可以使用文件对话框来选择保存的路径和文件名,然后将画板上的图形以文件的形式保存起来,并能够在需要的时候打开并显示在画板上。 通过以上步骤,便可以完成WinForm实现的仿Windows画板绘图。用户可以在画板上使用各种绘图工具来创作和编辑图形,还可以保存和打开绘制的图形文件。这样的应用程序可以满足用户对于简单绘图的需求,并且通过WinForm的可视化设计,可以方便快捷地操作和管理画板上的图形。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值