using System;
using System.Collections;
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.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace real课程设计
{
public partial class Form1 : Form
{
Graphics g; //image的
Graphics graphics;//PictureBox1 的 Graphics类封装了一个GDI+绘图图面,提供将对象绘制到显示到设备的方法(画板)
System.Drawing.Image image; //显示的图片
Point StartPoint; Point EndPoint;
Stack<Bitmap> list = new Stack<Bitmap> ( );
Stack<Bitmap> relist = new Stack<Bitmap> ( );
Pen[] pen = new Pen[3];//三个颜色 红绿蓝
int 颜色下标;//0-2种 3颜色
float 画笔粗细 = 1; //表示绘制时候线的粗细,默认值为1磅
int type = 0;
bool canselect图片= false;
public Form1 ( )
{
InitializeComponent ( );
}
private void Form1_Load ( object sender, EventArgs e )
{
graphics = pictureBox1.CreateGraphics ( );
pictureBox1.BackColor = Color.White;
image = new Bitmap ( pictureBox1.Width, pictureBox1.Height );
g = Graphics.FromImage ( image );
g.Clear ( Color.White );
graphics.Clear ( Color.White );
初始化颜色 ( );
pictureBox1.Image = image;
pictureBox1.Enabled = true;
}
public void 初始化颜色 ( )//改变颜色的方法
{
pen[0] = new Pen ( Color.Red, 画笔粗细 );
pen[1] = new Pen ( Color.Green, 画笔粗细 );
pen[2] = new Pen ( Color.Blue, 画笔粗细 );
}
//造个矩形
private Rectangle MakeRectangle ( Point p1, Point p2 )
{
int top, left, bottom, right;
top = p1.Y <= p2.Y ? p1.Y : p2.Y;//计算矩形左上角点的y坐标
left = p1.X <= p2.X ? p1.X : p2.X;//计算矩形左上角点的x坐标
bottom = p1.Y > p2.Y ? p1.Y : p2.Y;//计算矩形右下角点的y坐标
right = p1.X > p2.X ? p1.X : p2.X;//计算矩形右下角点的x坐标
return (new Rectangle ( left, top, right - left, bottom - top ));//返回矩形
}
//鼠标按下
private void pictureBox1_MouseDown ( object sender, MouseEventArgs e )
{
if (e.Button == MouseButtons.Left)
{
StartPoint.X = e.X;
StartPoint.Y = e.Y;
EndPoint.X = e.X;
EndPoint.Y = e.Y;
}
}
//鼠标释放
private void pictureBox1_MouseUp ( object sender, MouseEventArgs e )
{
if (canselect图片)
{
Stack<画图类> tempStack = new Stack<画图类> ();
foreach (画图类 t in 画图类.stack__)
{
if (Math.Abs ( t.StartPoint.X - e.X ) <= 10 &&
Math.Abs ( t.StartPoint.Y - e.Y ) <= 10)
{
t.colorindex = 颜色下标;
}
tempStack.Push( t );
}
画图类.stack__.Clear ( );
foreach (var item in tempStack)
{
画图类.stack__.Push ( item );
}
DrawAll ( );
return;
}
if (type == 0)
{
EndPoint.X = e.X;
EndPoint.Y = e.Y;
Rectangle r1 = MakeRectangle ( StartPoint, EndPoint );
g.DrawEllipse ( pen[颜色下标], r1 );
pictureBox1.Image = image;
}
if (type == 1)
{
EndPoint.X = e.X;
EndPoint.Y = e.Y;
Rectangle r2 = MakeRectangle ( StartPoint, EndPoint );
g.DrawRectangle ( pen[颜色下标], r2 );
pictureBox1.Image = image;
}
if (type == 2)
{
EndPoint.X = e.X;
EndPoint.Y = e.Y;
g.DrawLine ( pen[颜色下标],StartPoint,EndPoint );
pictureBox1.Image = image;
}
画图类.stack__.Push ( new 画图类( StartPoint ,EndPoint,type, 颜色下标) );
}
private void 保存_Click ( object sender, EventArgs e )
{
画图类.SaveStack ("temp" );
}
private void Red_ButtonClick ( object sender, EventArgs e )
{
颜色下标 = 0;
}
private void Green_ButtonClick ( object sender, EventArgs e )
{
颜色下标 = 1;
}
private void Blue_ButtonClick ( object sender, EventArgs e )
{
颜色下标 = 2;
}
private void 撤销ToolStripMenuItem_Click_1 ( object sender, EventArgs e )
{
画图类 画图__ = 画图类.stack__.Pop ();
画图类.restack__.Push ( 画图__ );
DrawAll ( );
}
private void 重做ToolStripMenuItem_Click ( object sender, EventArgs e )
{
画图类 画图__ = 画图类.restack__.Pop ( );
画图类.stack__.Push ( 画图__ );
DrawAll ( );
}
private void 圆和椭圆ToolStripMenuItem_Click ( object sender, EventArgs e )
{
type = 0;
}
private void 长方形和矩形ToolStripMenuItem_Click ( object sender, EventArgs e )
{
type = 1;
}
private void 线条ToolStripMenuItem1_Click ( object sender, EventArgs e )
{
type = 2;
}
private void 放大函数 ( object sender, EventArgs e )
{
foreach (画图类 t in 画图类.stack__)
{
t.StartPoint.X = (int)(t.StartPoint.X * 1.08);
t.StartPoint.Y = (int)(t.StartPoint.Y * 1.08);
t.EndPoint.X = (int)(t.EndPoint.X * 1.08);
t.EndPoint.Y = (int)(t.EndPoint.Y * 1.08);
}
DrawAll ( );
}
private void 缩小函数 ( object sender, EventArgs e )
{
foreach (画图类 t in 画图类.stack__)
{
t.StartPoint.X = (int)(t.StartPoint.X * 0.95);
t.StartPoint.Y = (int)(t.StartPoint.Y * 0.95);
t.EndPoint.X = (int)(t.EndPoint.X * 0.95);
t.EndPoint.Y = (int)(t.EndPoint.Y * 0.95);
}
DrawAll ( );
}
private void DrawAll ( )
{
g.Clear ( Color.White );
foreach (画图类 t in 画图类.stack__)
{
if (t.type == 0)
{
Rectangle r1 = MakeRectangle ( t.StartPoint, t.EndPoint );
g.DrawEllipse ( pen[t.colorindex], r1 );
pictureBox1.Image = image;
}
if (t.type == 1)
{
Rectangle r2 = MakeRectangle ( t.StartPoint, t.EndPoint );
g.DrawRectangle ( pen[t.colorindex], r2 );
pictureBox1.Image = image;
}
if (t.type == 2)
{
g.DrawLine ( pen[t.colorindex], t.StartPoint,t.EndPoint );
pictureBox1.Image = image;
}
}
}
private void 切换能否选中 ( object sender, EventArgs e )
{
canselect图片 = canselect图片?false:true;
}
private void 加载function ( object sender, EventArgs e )
{画图类.LoadStack ("temp" );
DrawAll ( );
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace real课程设计
{
[Serializable] // 添加可序列化特性
internal class 画图类
{
public Point StartPoint;
public Point EndPoint;
public int type;
public int colorindex;
public static Stack<画图类> stack__ = new Stack<画图类> ( );//包含本图
public static Stack<画图类> restack__ = new Stack<画图类> ( );
public 画图类 ( Point StartPoint, Point EndPoint, int type, int colorindex )
{
this.StartPoint = StartPoint;
this.EndPoint = EndPoint;
this.type = type;
this.colorindex = colorindex;
}
// 保存 stack__ 到文件
public static void SaveStack ( string filename )
{
try
{
using (Stream stream = File.Open ( filename, FileMode.Create ))
{
BinaryFormatter binaryFormatter = new BinaryFormatter ( );
binaryFormatter.Serialize ( stream, stack__ );
}
}
catch (IOException ex)
{
Console.WriteLine ( "保存失败:" + ex.Message );
}
}
// 从文件中读取 stack__
public static void LoadStack ( string filename )
{
try
{
using (Stream stream = File.Open ( filename, FileMode.Open ))
{
BinaryFormatter binaryFormatter = new BinaryFormatter ( );
stack__ = (Stack<画图类>)binaryFormatter.Deserialize ( stream );
}
}
catch (IOException ex)
{
Console.WriteLine ( "读取失败:" + ex.Message );
}
catch (Exception ex)
{
Console.WriteLine ( "反序列化失败:" + ex.Message );
}
}
}
}