C#画图小程序.doc_第1页
C#画图小程序.doc_第2页
C#画图小程序.doc_第3页
C#画图小程序.doc_第4页
C#画图小程序.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

图像编辑软件班级:网工1班 姓名:张田新 学号:1225114042 完成日期:2014.05.16一、需求分析a) 画线类型,即绘图时所采用的线型,包括:实线、点线、长划线、点划线、双点划线等b) 绘图类型,即不同的绘图形状,包括:直线、椭圆、矩形c) 填充类型,即画刷模式下的形状填充,包括:实心、十字、斜纹等d) 画线颜色修改(按钮控件),弹出ColorDialoge) 清空按钮(按钮控件)f) 画线宽度(NumericUpDown控件)g) 画刷模式(复选框控件)h) 状态栏(在绘画时显示当前光标所处坐标位置)i) 其它:组合框、控件标题属性等二、框架设计1、窗口Form1 public partial class MainForm : Form public MainForm() InitializeComponent(); Form2public partial class Form2 : Form public Form2() InitializeComponent(); 2、设定数据类型定义:Pen MyPen = new System.Drawing.Pen(System.Drawing.Color.SaddleBrown, 2);/定义画笔,并且初始化颜色和宽度 PointF pStart;/开始点坐标 PointF pEnd;/结束点坐标 PointF pWork;/移动点坐标 Boolean working;/运动中 Boolean brushmode;/画刷 String dmode = line; ColorDialog ColorDialog1 = new ColorDialog();/颜色对话框 Bitmap myBitmap = new Bitmap(194, 294);/ 位图上绘制图形,创建位图,大小为PictureBox1的尺寸 private int tickcounts;/计时器3、基本操作:Form1:自定义DrawShape函数,用于实现画图类型的实现void DrawShape()MainForm_Load用于初始化绘图类型和画笔颜色private void MainForm_Load(object sender, EventArgs e)在PictureBox1中添加MouseDown事件,用于记录绘图的起点private void PictureBox1_MouseDown_1(object sender, MouseEventArgs e)在PictureBox1中添加MouseMove事件,显示鼠标的位置,并画连续线private void PictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)在PictureBox1中添加MouseUp事件,用于设置绘图终点并开始绘图private void PictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)button1的Click事件,用于设置画笔颜色,显示颜色对话框private void button1_Click(object sender, EventArgs e)BtnClear的Click事件,用PictureBox1的背景颜色清空绘图区域和位图private void BtnClear_Click(object sender, EventArgs e)checkBox2_CheckedChanged事件,用于设置启动定时器private void checkBox2_CheckedChanged_1(object sender, EventArgs e)在定时器控件Tick事件中添加响应代码,用于改变状态栏颜色private void timer1_Tick_1(object sender, EventArgs e)右键剪切功能private void cutToolStripMenuItem_Click(object sender, EventArgs e)右键粘贴功能private void pasteToolStripMenuItem_Click(object sender, EventArgs e)右键清空功能private void clearToolStripMenuItem_Click(object sender, EventArgs e)右键保存,需要先添加saveFileDialog对话框,显示该对话框private void saveFileDialog2_FileOk(object sender, CancelEventArgs e)右键about点击事件,显示新的窗体,需要新建Form2private void aboutToolStripMenuItem_Click(object sender, EventArgs e)给pctureBox1添加paint事件,使窗口重绘后内容不消失private void pictureBox1_Paint(object sender, PaintEventArgs e)Form2:Label控件、panellinkLabel1_LinkClicked用于点击链接相应的网址三、实现细节private void PictureBox1_MouseUp(object sender, MouseEventArgs e) if (e.Button = MouseButtons.Left) if (working = true) pEnd.X = e.X; pEnd.Y = e.Y; MyPen.Color = button1.BackColor; MyPen.Width = (int)numericUpDown1.Value; brushmode = checkBox1.Checked;/a)画线类型,即绘图时所采用的线型,包括:实线、点线、长划线、点划线、双点划线等 if (rbShiline.Checked = true) MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; if (rbDianline.Checked = true) MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; if (rbChanghualine.Checked = true) MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; if (rbZidingyi.Checked = true) MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom; if (rbShuangdianline.Checked = true) MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot; if (rbDianhualine.Checked = true) MyPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;/b)绘图类型,即不同的绘图形状,包括:直线、椭圆、矩形 if (rbZhiline.Checked = true) dmode = line; if (rbTuoyuan.Checked = true) dmode = circle; if (rbJuxing.Checked = true) dmode = rect; if (rbXiewen.Checked = true) MyPen.Brush = new System.Drawing.Drawing2D.HatchBrush( System.Drawing.Drawing2D.HatchStyle.DarkDownwardDiagonal, MyPen.Color);/c)填充类型,即画刷模式下的形状填充,包括:实心、十字、斜纹等 if (rbShizi.Checked = true) MyPen.Brush = new System.Drawing.Drawing2D.HatchBrush( System.Drawing.Drawing2D.HatchStyle.Cross, MyPen.Color); if (rbShixin.Checked = true) MyPen.Brush = new SolidBrush(MyPen.Color); DrawShape(PictureBox1.CreateGraphics(), pStart, pEnd, brushmode, MyPen, dmode); working = false; Case:画直线switch (dmode) case line: g.DrawLine(MyPen, pStart, pEnd); e.DrawLine(penmode, startp.X, startp.Y, endp.X, endp.Y); break;case:矩形:case rect:/当第二点的X或Y坐标小于第一点时,矩形无法绘制的问题(bug) if (int)endp.Y (int)startp.Y & (int)endp.X (int)startp.X) if (brushmode = true) g.FillRectangle(MyPen.Brush, rect1); e.FillRectangle(penmode.Brush, rect1); else g.DrawRectangle(MyPen, rect1); e.DrawRectangle(penmode, rect1); /当第二点的Y坐标小于第一点时,矩形无法绘制的问题 if (int)endp.Y (int)startp.X) if (brushmode = true) g.FillRectangle(MyPen.Brush, rect2); e.FillRectangle(penmode.Brush, rect2); else g.DrawRectangle(MyPen, rect2); e.DrawRectangle(penmode, rect2); /当第二点的X坐标小于第一点时,矩形无法绘制的问题 if (int)endp.Y (int)startp.Y & (int)endp.X (int)startp.X) if (brushmode = true) g.FillRectangle(MyPen.Brush, rect3); e.FillRectangle(penmode.Brush, rect3); else g.DrawRectangle(MyPen, rect3); e.DrawRectangle(penmode, rect3); /当第二点的X和Y坐标小于第一点时,矩形无法绘制的问题 if (int)endp.Y (int)startp.Y & (int)endp.X (int)startp.X) if (brushmode = true) g.FillRectangle(MyPen.Brush, rect4); e.FillRectangle(penmode.Brush, rect4); else g.DrawRectangle(MyPen, rect4); e.DrawRectangle(penmode, rect4); break;case画椭圆: case circle: if (brushmode = true) g.FillEllipse(MyPen.Brush, rect1); e.FillEllipse(penmode.Brush, rect1); else g.DrawEllipse(MyPen, rect1); e.DrawEllipse(penmode, rect1); break;d)画线颜色修改(按钮控件),弹出ColorDialogprivate void button1_Click_1(object sender, EventArgs e) ColorDialog1.ShowDialog(); button1.BackColor = ColorDialog1.Color; e)清空按钮(按钮控件)private void BtnClear_Click(object sender, EventArgs e) PictureBox1.CreateGraphics().Clear(PictureBox1.BackColor); Graphics g = Graphics.FromImage(myBitmap); g.Clear(PictureBox1.BackColor); f)画线宽度(NumericUpDown控件) MyPen.Width = (int)numericUpDown1.Value;g)画刷模式(复选框控件)if (brushmode = true) g.FillRectangle(MyPen.Brush, rect1);/利用位图画矩形 e.FillRectangle(penmode.Brush, rect1); h)状态栏(在绘画时显示当前光标所处坐标位置)private void checkBox2_CheckedChanged_1(object sender, EventArgs e) tickcounts = 0; if (checkBox2.Checked = true) timer1.Start();/启动定时器 timer1.Enabled = true; private void timer1_Tick(object sender, EventArgs e) if (tickcounts % 4 = 0)/第一个颜色 statusStrip1.BackColor = arrColor0; if (tickcounts % 4 = 1) statusStrip1.BackColor = arrColor1; arrColor0.ToString(); if (tickcounts % 4 = 2) statusStrip1.BackColor = arrColor2; if (tickcounts % 4 = 3) statusStrip1.BackColor = arrColor3; arrColor1.ToString(); tickcounts+; j) 其它:组合框、控件标题属性等2. 教材原例Bug修改(15分)a) 窗口刷新后图像消失问题/给pctureBox1添加paint事件,使窗口重绘后内容不消失 private void PictureBox1_Paint(object sender, PaintEventArgs e) e.Graphics.DrawImage(myBitmap, 0, 0); b) 画矩形:当第二点的X或Y坐标小于第一点时,矩形无法绘制的问题当第二点的Y坐标小于第一点时,矩形无法绘制的问题 Rectangle rect2 = new Rectangle(int)startp.X, (int)endp.Y, (int)(endp.X - startp.X), (int)(startp.Y - endp.Y); if (int)endp.Y (int)startp.X) if (brushmode = true) g.FillRectangle(MyPen.Brush, rect2); e.FillRectangle(penmode.Brush, rect2); else g.DrawRectangle(MyPen, rect2); e.DrawRectangle(penmode, rect2); 其他两种情况类似处理,这里不再赘述c) 鼠标的响应:左键右键均响应,应修改为仅响应鼠标左键进行绘图操作例如:if (e.Button = MouseButtons.Left) /仅响应鼠标左键 working = true; pStart.X = e.X; pStart.Y = e.Y; toolStripStatusLabel1.Text = + e.X + , + e.Y + ; d) 初始颜色问题,原初始颜色与背景色相同,无法显示图像Pen MyPen = new System.Drawing.Pen(System.Drawing.Color.SaddleBrown, 2);System.Drawing.Drawing2D.HatchStyle.DarkDownwardDiagonal, Color.SaddleBrown);e) 清空功能:用原PictureBox背景来清空而不用Color属性所代表的颜色来清空绘图区 private void BtnClear_Click_1(object sender, EventArgs e) PictureBox1.CreateGraphics().Clear(PictureBox1.BackColor);/清空绘图区域 Graphics g = Graphics.FromImage(myBitmap); g.Clear(PictureBox1.BackColor);/清空位图 3. 特色功能a) 连续线:即通常采用的画笔画出连续线条(20分) /在PictureBox1中添加MouseMove事件,显示鼠标的位置,并画连续线 private void PictureBox1_MouseMove(object sender, MouseEventArgs e) Graphics g = Graphics.FromImage(myBitmap);/位图 Graphics h = PictureBox1.CreateGraphics(); if (e.Button = MouseButtons.Left) if (working = true) pWork.X = e.X; pWork.Y = e.Y; toolStripStatusLabel1.Text = + e.X + , + e.Y + ; if (rbLianxuline.Checked = true) /画连续线 g.DrawLine(MyPen, pStart, pWork); /位图 h.DrawLine(MyPen, pStart.X, pStart.Y, pWork.X, pWork.Y); /pictureBox1 pStart.X = pWork.X;/将上一个终点作为新的起点 pStart.Y = pWork.Y; b) timer复选框:实现间隔的改变状态条背景色(10分) /checkBox2_CheckedChanged事件,用于设置启动定时器private void checkBox2_CheckedChanged_1(object sender, EventArgs e) tickcounts = 0; if (checkBox2.Checked = true) timer1.Start();/启动定时器 timer1.Enabled = true; /改变复选框颜色 private void timer1_Tick(object sender, EventArgs e) if (tickcounts % 4 = 0)/第一?个?颜?色? statusStrip1.BackColor = arrColor0; if (tickcounts % 4 = 1) statusStrip1.BackColor = arrColor1; arrColor0.ToString(); if (tickcounts % 4 = 2) statusStrip1.BackColor = arrColor2; if (tickcounts % 4 = 3) statusStrip1.BackColor = arrColor3; arrColor1.ToString(); tickcounts+; c) 右键弹出上下文菜单:剪切:完成剪切图像功能(5分)/-右键的剪切功能- private void cutToolStripMenuItem_Click(object sender, EventArgs e) Clipboard.SetDataObject(myBitmap, true); PictureBox1.CreateGraphics().Clear(PictureBox1.BackColor);/pictureBox1绘图区域清空 Graphics g = Graphics.FromImage(myBitmap); g.Clear(PictureBox1.BackColor);/位图清空 粘贴:完成图像粘贴(5分)/-右键粘贴功能- private void pasteToolStripMenuItem_Click(object sender, EventArgs e) IDataObject clipboard_data; clipboard_data

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论