C# 我的小画板
2021-05-13 11:29
标签:main sem generic src strip namespace pac ase position 我的画板 先看实现图 实现过程 金燕电子 C# 我的小画板 标签:main sem generic src strip namespace pac ase position 原文地址:http://www.cnblogs.com/guyandianzi/p/7550915.htmlusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//**********************金燕电子****************************************
namespace huaban
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
toolStrip1.Enabled = false;
foreColor = Color.Black ;
backColor = Color.White ;
}
Color backColor = new Color();
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(backColor);
toolStrip1.Enabled = true;
//创建一个Bitmap
theImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);
editFileName = "新建文件";
//修改窗口标题
this.Text = "MyDraw\t" + editFileName;
ig = Graphics.FromImage(theImage);
ig.Clear(backColor);
}
string editFileName = "";
private Image theImage;
public Graphics ig { get; private set; }
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//修改窗口标题
this.Text = "MyDraw\t" + openFileDialog1.FileName;
editFileName = openFileDialog1.FileName;
theImage = Image.FromFile(openFileDialog1.FileName);
Graphics g = this.CreateGraphics();
g.DrawImage(theImage, this.ClientRectangle);
ig = Graphics.FromImage(theImage);
ig.DrawImage(theImage, this.ClientRectangle);
//ToolBar可以使用了
toolStrip1.Enabled = true;
}
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "图像(*.bmp)|*.bmp";
saveFileDialog1.FileName = editFileName;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
theImage.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
this.Text = "MyDraw\t" + saveFileDialog1.FileName;
editFileName = saveFileDialog1.FileName;
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
//将Image中保存的图像,绘制出来
Graphics g = this.CreateGraphics();
if (theImage != null)
{
g.Clear(Color.White);
g.DrawImage(theImage, this.ClientRectangle);
}
}
Color foreColor = new Color();
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//如果选择文字输入,则打开strInput窗体
if (drawTool == (int)drawTools.String)
{
Frm_Text inputBox = new Frm_Text();
inputBox.StartPosition = FormStartPosition.CenterParent;
if (inputBox.ShowDialog() == DialogResult.OK)
{
Graphics g = this.CreateGraphics();
Font theFont = this.Font;
g.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
ig.DrawString(inputBox.textBox1.Text, theFont, new SolidBrush(foreColor), e.X, e.Y);
}
}
//如果开始绘制,则开始记录鼠标位置
else if ((isDrawing = !isDrawing) == true)
{
startPoint = new Point(e.X, e.Y);
oldPoint = new Point(e.X, e.Y);
}
}
}
int drawTool = 0;
enum drawTools
{
None = 0,
Pen = 1,
Line = 2,
Ellipse = 3,
Rectangle = 4,
String = 5,
Rubber =6
}
Point oldPoint = new Point();
Point startPoint = new Point();
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Graphics g;
g = this.CreateGraphics();
if (isDrawing)
{
switch (drawTool)
{
case (int)drawTools.None:
break;
case (int)drawTools.Pen:
//从上一个点到当前点绘制线段
g.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
ig.DrawLine(new Pen(foreColor, 1), oldPoint, new Point(e.X, e.Y));
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
case (int)drawTools.Line:
//首先恢复此次操作之前的图像,然后再添加Line
this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
break;
case (int)drawTools.Ellipse:
//首先恢复此次操作之前的图像,然后再添加Ellipse
this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case (int)drawTools.Rectangle:
//首先恢复此次操作之前的图像,然后再添加Rectangle
this.Frm_Main_Paint(this, new PaintEventArgs(this.CreateGraphics(), this.ClientRectangle));
g.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case (int)drawTools.String:
break;
case (int)drawTools.Rubber:
//用背景色绘制宽线段
g.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
ig.DrawLine(new Pen(backColor, 20), oldPoint, new Point(e.X, e.Y));
oldPoint.X = e.X;
oldPoint.Y = e.Y;
break;
}
}
}
bool isDrawing = false;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isDrawing = false;
switch (drawTool)
{
case (int)drawTools.Line:
ig.DrawLine(new Pen(foreColor, 1), startPoint, new Point(e.X, e.Y));
break;
case (int)drawTools.Ellipse:
ig.DrawEllipse(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
case (int)drawTools.Rectangle:
ig.DrawRectangle(new Pen(foreColor, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
break;
}
}
private void Frm_Main_Paint(object sender, PaintEventArgs e)
{
//将Image中保存的图像,绘制出来
Graphics g = this.CreateGraphics();
if (theImage != null)
{
g.Clear(Color.White);
g.DrawImage(theImage, this.ClientRectangle);
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton6.Checked = false;
toolStripButton2.Checked = false;
toolStripButton3.Checked = false;
toolStripButton4.Checked = false;
toolStripButton5.Checked = false;
if (drawTool == (int)drawTools.Pen)
{
drawTool = (int)drawTools.None ;
toolStripButton1.Checked = false ;
}
else
{
drawTool = (int)drawTools.Pen;
toolStripButton1.Checked = true;
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
toolStripButton1.Checked = false;
toolStripButton6.Checked = false;
toolStripButton3.Checked = false;
toolStripButton4.Checked = false;
toolStripButton5.Checked = false;
if (drawTool == (int)drawTools.Line)
{
drawTool = (int)drawTools.None;
toolStripButton2.Checked = false;
}
else
{
drawTool = (int)drawTools.Line;
toolStripButton2.Checked = true;
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
toolStripButton1.Checked = false;
toolStripButton2.Checked = false;
toolStripButton6.Checked = false;
toolStripButton4.Checked = false;
toolStripButton5.Checked = false;
if (drawTool == (int)drawTools.Rectangle)
{
drawTool = (int)drawTools.None;
toolStripButton3.Checked = false;
}
else
{
drawTool = (int)drawTools.Rectangle;
toolStripButton3.Checked = true;
}
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
toolStripButton1.Checked = false;
toolStripButton2.Checked = false;
toolStripButton3.Checked = false;
toolStripButton6.Checked = false;
toolStripButton5.Checked = false;
if (drawTool == (int)drawTools.Ellipse)
{
drawTool = (int)drawTools.None;
toolStripButton4.Checked = false;
}
else
{
drawTool = (int)drawTools.Ellipse;
toolStripButton4.Checked = true;
}
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
toolStripButton1.Checked = false;
toolStripButton2.Checked = false;
toolStripButton3.Checked = false;
toolStripButton4.Checked = false;
toolStripButton6.Checked = false;
if (drawTool == (int)drawTools.String)
{
drawTool = (int)drawTools.None;
toolStripButton5.Checked = false;
}
else
{
drawTool = (int)drawTools.String;
toolStripButton5.Checked = true;
}
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
toolStripButton1.Checked = false;
toolStripButton2.Checked = false;
toolStripButton3.Checked = false;
toolStripButton4.Checked = false;
toolStripButton5.Checked = false;
if (drawTool == (int)drawTools.Rubber )
{
drawTool = (int)drawTools.None;
toolStripButton6.Checked = false;
}
else
{
drawTool = (int)drawTools.Rubber;
toolStripButton6.Checked = true;
}
}
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
foreColor = colorDialog1.Color ;
}
}
}
}