c# 一个简单的画板,可以设置背景图,自动保存绘图步骤,手动撤销上一次改动。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;

public partial class MyDrawBoxCtrl : PictureBox
{
	public MyDrawBoxCtrl()
	{
		InitializeComponent();
	}
	
	public bool IsDraw
	{
		get { return isdraw; }
		set { isdraw = value; }
	}

	bool isdraw = false;
	bool init = false;
	protected override void OnCreateControl()
	{
		base.OnCreateControl();
	}

	protected override void OnSizeChanged(EventArgs e)
	{
		base.OnSizeChanged(e);
		if (!init)
		{
			img = new Bitmap(this.BackgroundImage, this.Width, this.Height);
			grap = Graphics.FromImage(img);
			this.Image = img;
			init = true;
		}
	}
	
	public Color DrawColor
	{
		get { return _drawcolor; }
		set { _drawcolor = value; }
	}

	public int LineWidth
	{
		get { return _linewidth; }
		set { _linewidth = value; }
	}

	private int _linewidth = 2;
	private Graphics grap;
	private Image img = null;
	private List<Image> list = new List<Image>();
	private Color _drawcolor = Color.Blue;
	private Point p1, p2;//定义两个点(启点,终点)
	private static bool drawing = false;//设置一个启动标志
	protected override void OnMouseDown(MouseEventArgs e)
	{
		base.OnMouseDown(e);
		p1 = new Point(e.X, e.Y);
		p2 = new Point(e.X, e.Y);
		drawing = true;
	}

	protected override void OnMouseMove(MouseEventArgs e)
	{
		base.OnMouseMove(e);
		if (this.Enabled)
		{
			if (e.Button == MouseButtons.Left)
			{
				if (drawing)
				{
					Point currentPoint = new Point(e.X, e.Y);
					Graphics g = this.CreateGraphics();
					g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//消除锯齿
					g.TextRenderingHint = TextRenderingHint.AntiAlias;
					g.DrawLine(new Pen(_drawcolor, _linewidth), p2, currentPoint);
					grap.DrawLine(new Pen(_drawcolor, _linewidth), p2, currentPoint);
					p2.X = currentPoint.X;
					p2.Y = currentPoint.Y;
				}
			}
		}
	}

	protected override void OnMouseUp(MouseEventArgs e)
	{
		drawing = false;
		base.OnMouseUp(e);
		if (this.Enabled)
		{
			try
			{
				isdraw = true;
				this.Image = img;
				if (list.Count < 30)  //最大保存30次画图操作,这里可适当调大
                {
                    list.Add((Image)img.Clone());
                }
                else
                {
                    list.RemoveAt(1);
                    list.Add((Image)img.Clone());
                }
			}
			catch
			{
				System.Diagnostics.Debug.WriteLine("Image为空");
			}
		}
	}

	/// <summary>
	/// 绘图前加载背景图
	/// </summary>
	/// <param name="dataImage"></param>
	public void Load(Image dataImage)
	{
		img = new Bitmap(dataImage, this.Width, this.Height);
		grap = Graphics.FromImage(img);
		this.Image = img;
		list.Add((Image)dataImage.Clone());
		isdraw = true;
	}

	/// <summary>
	/// 撤销上一次的绘图
	/// </summary>
	public void Undo()
	{
		if (!this.Enabled)
			return;
		if (list.Count > 1)
		{
			list.RemoveAt(list.Count - 1);
			img = new Bitmap(list[list.Count - 1], this.Width, this.Height);
		}
		else
		{
			list.Clear();
			img = new Bitmap(this.BackgroundImage, this.Width, this.Height);
		}
		grap = Graphics.FromImage(img);
		this.Image = img;
	}

	/// <summary>
	/// 清除所有绘图
	/// </summary>
	public void Clear()
	{
		if (!this.Enabled)
			return;
		this.Refresh();
		list.Clear();
		img = new Bitmap(this.BackgroundImage, this.Width, this.Height);
		grap = Graphics.FromImage(img);
		this.Image = img;
	}
}

 

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值