项目模板:要实现在背景和无背景上完成画线,画直线、矩形、椭圆、并能随意调整字体的大小
首先要定义绘制的类型
enum DrawMode
{None, // 没有选择绘制型Pen, // 画笔 画直线Line,// 画直线Rectangle,// 画矩形Ellipse, // 画椭圆Rubber // 橡皮擦
}
//如果要想绘制成图片可以使用picturebox作为载体,
//之前使用g.drawLine只是绘制到窗体,只是表面现象,并没有绘制成图片
//把绘制的效果形成一个图片使用drawImage方法
全局变量:
Graphics g; //绘制对象Graphics imgGra;// 把绘制出效果形成一个图片Color curColor = Color.Black;//设置画笔的颜色int penSize = 0;// 笔刷粗细string title; // 窗体的标题DrawMode curMode = DrawMode.None;//当前绘制的类型Image backImg;// 背景图片
设置下拉选项
g = this.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;toolStripComboBox1.SelectedIndex = 1; //设置下拉框默认值选择this.Text = "画板";
选择背景
private void 选择文件ToolStripMenuItem_Click(object sender, EventArgs e)
{OpenFileDialog ofd = new OpenFileDialog();// 文本选择对话框ofd.Filter = "png图片|*.png|jpg图片|*.jpg"; //过滤文件类型ofd.Multiselect = false;//是否支持多选,if (ofd.ShowDialog() == DialogResult.OK){Image sourceImage = Image.FromFile(ofd.FileName);// 加载对应路径的资源 把图片绘制的背景//this.ClientRectangle.Width窗体的宽度//this.ClientRectangle.Height窗体的高度backImg = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);//设置背景图片大小imgGra = Graphics.FromImage(backImg);// 把backImage作为绘制背景图// 把图片绘制到管理背景图的图像上imgGra//imgGra.DrawImage()g.Clear(Color.White); //把绘制区域以白色填充,目的先把之前的背景清空掉g.DrawImage(sourceImage, this.ClientRectangle);// 绘制图片,参数1是绘制的图片 参数2是绘制的区域this.Text = title + "\t" + ofd.FileName;}}
无背景
private void 无背景ToolStripMenuItem_Click(object sender, EventArgs e)
{// 设置无背景的背景图宽度和高度backImg = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height); //设置一张图片imgGra = Graphics.FromImage(backImg);// 把图片作为绘制的背景imgGra.Clear(Color.White); // 清空背景g.DrawImage(backImg, this.ClientRectangle);//绘制图片this.Text = title + "\t" + "未命名";}
点击画笔pen的事件
private void toolStripLabel2_Click(object sender, EventArgs e)
{//只是修改绘制的类型,真正绘制的时候在鼠标移动方法进行绘制curMode = DrawMode.Pen;
}
鼠标按下事件
Point startPoint; // 起始点位置
bool drawFlag;// 控制是否开始绘制的变量
private void Form1_MouseDown(object sender, MouseEventArgs e)
{if(e.Button == MouseButtons.Left){startPoint = e.Location;// 把点击点坐标赋值给起始点drawFlag = true;}}
鼠标移动
private void Form1_MouseMove(object sender, MouseEventArgs e)
{if (drawFlag == false) return;switch (curMode) {case DrawMode.None: break;case DrawMode.Pen:// 粗细不要写定值 先写2g.DrawLine(new Pen(curColor, penSize), startPoint, e.Location);imgGra.DrawLine(new Pen(curColor, penSize), startPoint, e.Location);startPoint = e.Location;break;case DrawMode.Line:// 为了解决窗体最小化的时候出现 绘制的效果的消失的问题,//打开窗体重新调用绘制的方法,把backimage重新绘制到窗体上// 给窗体添加一个paint事件 把backimage重新绘制到窗体上Form1_Paint(null, null);g.DrawLine(new Pen(curColor, penSize), startPoint, e.Location);break;case DrawMode.Rectangle:Form1_Paint(null, null);g.DrawRectangle(new Pen(curColor, penSize), startPoint.X, startPoint.Y, e.Location.X - startPoint.X, e.Location.Y - startPoint.Y);break;case DrawMode.Ellipse:Form1_Paint(null, null);g.DrawEllipse(new Pen(curColor, penSize), startPoint.X, startPoint.Y, e.Location.X - startPoint.X, e.Location.Y - startPoint.Y);break;case DrawMode.Rubber:// 绘制一个白色的线就行g.DrawLine(new Pen(Color.White, 10), startPoint, e.Location);imgGra.DrawLine(new Pen(Color.White, 10), startPoint, e.Location);startPoint = e.Location;break;default: break;}}
鼠标松开
private void Form1_MouseUp(object sender, MouseEventArgs e){drawFlag = false;switch (curMode){case DrawMode.None:break;case DrawMode.Pen:break;case DrawMode.Line:imgGra.DrawLine(new Pen(curColor, penSize), startPoint, e.Location); break;case DrawMode.Rectangle:imgGra.DrawRectangle(new Pen(curColor, penSize), startPoint.X, startPoint.Y, e.Location.X - startPoint.X, e.Location.Y - startPoint.Y);break;case DrawMode.Ellipse:imgGra.DrawEllipse(new Pen(curColor, penSize), startPoint.X, startPoint.Y, e.Location.X - startPoint.X, e.Location.Y - startPoint.Y);break;case DrawMode.Rubber:break;default:break;}}
修改笔刷的颜色
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{//FontDialogColorDialog cd = new ColorDialog();if (cd.ShowDialog() == DialogResult.OK){curColor = cd.Color; //修改画笔颜色变量}}
修改笔刷大小的事件
private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{penSize = Convert.ToInt16( toolStripComboBox1.Text);
}
修改绘制类型
private void toolStripLabel3_Click(object sender, EventArgs e)
{curMode = DrawMode.Line; //绘制直线
}
矩形
private void toolStripLabel4_Click(object sender, EventArgs e)
{//矩形curMode = DrawMode.Rectangle;
}
椭圆
private void toolStripLabel5_Click(object sender, EventArgs e)
{curMode = DrawMode.Ellipse;//椭圆
}
在改事件中重新绘制图片
private void Form1_Paint(object sender, PaintEventArgs e)
{if (backImg == null){return;}g.DrawImage(backImg, this.ClientRectangle);}
private void toolStripLabel6_Click(object sender, EventArgs e)
{curMode = DrawMode.Rubber;//
}