《2022年C#画图小程序 .pdf》由会员分享,可在线阅读,更多相关《2022年C#画图小程序 .pdf(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、图像编辑软件班级:网工 1 班姓名:张田新学号:1225114042 完成日期:2014.05.16 一、需求分析a)画线类型,即绘图时所采用的线型,包括:实线、点线、长划线、点划线、双点划线等b)绘图类型,即不同的绘图形状,包括:直线、椭圆、矩形c)填充类型,即画刷模式下的形状填充,包括:实心、十字、斜纹等d)画线颜色修改(按钮控件) ,弹出 ColorDialog e)清空按钮(按钮控件)f)画线宽度( NumericUpDown 控件)g)画刷模式(复选框控件)h)状态栏(在绘画时显示当前光标所处坐标位置)i)其它:组合框、控件标题属性等二、框架设计1、窗口Form1 public pa
2、rtial class MainForm : Form public MainForm() InitializeComponent(); Form2 public 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;
3、/移动点坐标 Boolean working;/运动中名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 10 页 - - - - - - - - - Boolean brushmode;/画刷 String dmode = line; ColorDialog ColorDialog1 = new ColorDialog();/颜色对话框 Bitmap myBitmap = new Bitmap(194, 294);/位图上绘制图形,创建位图,大小为PictureBox1的尺
4、寸 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中添加 Mous
5、eMove 事件,显示鼠标的位置,并画连续线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(
6、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(objec
7、t 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 save
8、FileDialog2_FileOk(object sender, CancelEventArgs e) 右键 about 点击事件,显示新的窗体,需要新建Form2 private void aboutToolStripMenuItem_Click(object sender, EventArgs e) 给 pctureBox1 添加 paint事件,使窗口重绘后内容不消失名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 10 页 - - - - - - - - - pr
9、ivate void pictureBox1_Paint(object sender, PaintEventArgs e) Form2: Label 控件、 panel linkLabel1_LinkClicked用于点击链接相应的网址三、实现细节privatevoid 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.BackC
10、olor; 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 .D
11、ot; 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
12、 (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 ) 名师资料总结 -
13、- -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 10 页 - - - - - - - - - MyPen.Brush = new System.Drawing.Drawing2D.HatchBrush ( System.Drawing.Drawing2D.HatchStyle .DarkDownwardDiagonal, MyPen.Color); /c) 填充类型,即画刷模式下的形状填充,包括:实心、十字、斜纹等if (rbShizi.Checked = true ) MyPen.Bru
14、sh = 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)
15、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.FillRectangl
16、e(penmode.Brush, rect1); else g.DrawRectangle(MyPen, rect1); e.DrawRectangle(penmode, rect1); / 当第二点的 Y坐标小于第一点时,矩形无法绘制的问题if ( int )endp.Y (int )startp.X) if (brushmode = true ) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 10 页 - - - - - - - - - g.FillRectangl
17、e(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(pen
18、mode.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
19、(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 ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - -
20、 - 名师精心整理 - - - - - - - 第 5 页,共 10 页 - - - - - - - - - d)画线颜色修改(按钮控件) ,弹出 ColorDialog privatevoid button1_Click_1(object sender, EventArgs e) ColorDialog1.ShowDialog(); button1.BackColor = ColorDialog1.Color; e)清空按钮(按钮控件)privatevoid BtnClear_Click(object sender, EventArgs e) PictureBox1.CreateGraphi
21、cs().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
22、)状态栏(在绘画时显示当前光标所处坐标位置)privatevoid checkBox2_CheckedChanged_1(object sender, EventArgs e) tickcounts = 0; if (checkBox2.Checked = true ) timer1.Start();/ 启动定时器 timer1.Enabled = true ; privatevoid timer1_Tick(object sender, EventArgs e) if (tickcounts % 4 = 0)/ 第一个颜色 statusStrip1.BackColor = arrColor0
23、; if (tickcounts % 4 = 1) statusStrip1.BackColor = arrColor1; arrColor0.ToString(); if (tickcounts % 4 = 2) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 10 页 - - - - - - - - - statusStrip1.BackColor = arrColor2; if (tickcounts % 4 = 3) statusStrip1.BackColor
24、= arrColor3; arrColor1.ToString(); tickcounts+; j)其它:组合框、控件标题属性等2. 教材原例 Bug修改( 15 分)a)窗口刷新后图像消失问题/ 给 pctureBox1 添加 paint 事件,使窗口重绘后内容不消失privatevoid PictureBox1_Paint(object sender, PaintEventArgs e) e.Graphics.DrawImage(myBitmap, 0, 0); b)画矩形:当第二点的X或 Y坐标小于第一点时,矩形无法绘制的问题当第二点的 Y坐标小于第一点时,矩形无法绘制的问题Rectan
25、gle 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(pen
26、mode, rect2); 其他两种情况类似处理,这里不再赘述c)鼠标的响应:左键右键均响应,应修改为仅响应鼠标左键进行绘图操作例如:if (e.Button = MouseButtons.Left)/ 仅响应鼠标左键 working = true ; pStart.X = e.X; pStart.Y = e.Y; toolStripStatusLabel1.Text = + e.X + , + e.Y + ; d)初始颜色问题,原初始颜色与背景色相同,无法显示图像名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 -
27、 - - - - - - 第 7 页,共 10 页 - - - - - - - - - Pen MyPen = new System.Drawing.Pen(System.Drawing.Color .SaddleBrown, 2); System.Drawing.Drawing2D.HatchStyle .DarkDownwardDiagonal, Color .SaddleBrown);e)清空功能:用原 PictureBox背景来清空而不用Color 属性所代表的颜色来清空绘图区privatevoid BtnClear_Click_1(object sender, EventArgs e
28、) PictureBox1.CreateGraphics().Clear(PictureBox1.BackColor); / 清空绘图区域 ?Graphics g = Graphics .FromImage(myBitmap); g.Clear(PictureBox1.BackColor);/ 清空位图 3. 特色功能a)连续线:即通常采用的画笔画出连续线条(20 分)/ 在PictureBox1 中添加 MouseMove 事件,显示鼠标的位置,并画连续线privatevoid PictureBox1_MouseMove( object sender, MouseEventArgs e) G
29、raphics 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); /
30、位图 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 事件,用于设置启动定时器privatevoid checkBox2_CheckedChanged_1(object sender, EventArgs e) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - -
31、- - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 10 页 - - - - - - - - - tickcounts = 0; if (checkBox2.Checked = true ) timer1.Start();/ 启动定时器 timer1.Enabled = true ; / 改变复选框颜色privatevoid timer1_Tick(object sender, EventArgs e) if (tickcounts % 4 = 0)/ 第2 一?个?颜?色|? statusStrip1.BackColor = arrColor0;
32、 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 分)/-右键的剪切功能 -privatevoid cutToolStri
33、pMenuItem_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 分)/-右键粘贴功能 -名师资料总结 - - -精品资料欢迎下载 - - - - -
34、 - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 10 页 - - - - - - - - - privatevoid pasteToolStripMenuItem_Click(object sender, EventArgs e) IDataObject clipboard_data; clipboard_data = Clipboard .GetDataObject(); if (clipboard_data.GetDataPresent(DataFormats .Bitmap) / 数据赋给位图 myBitmap = (Bit
35、map)clipboard_data.GetData(DataFormats .Bitmap); PictureBox1.CreateGraphics().DrawImage(myBitmap, 0, 0);/显示 清除: 同清空按钮,不再赘述保存:将所给图形保存为jpg 格式,弹出 SaveFileDialog对话框,可以指定文件名和路径进行保存 (10 分) / 右键保存,需要先添加saveFileDialog对话框,显示该对话框privatevoid saveImageToolStripMenuItem_Click(object sender, EventArgs e) saveFile
36、Dialog2.ShowDialog(); / 可以在 saveFileDialog2中 DefaultExt设置属性为: jpg / 在 filter设置为:图像文件 |* .jpg|所有文件 (*.*)|*.* About: 弹出关于对话框 (5 分) ,必须实现,填上每个人的自己的信息,/ 右键about 点击事件,需要新建 Form2privatevoid aboutToolStripMenuItem_Click(object sender, EventArgs e) Form2 form = new Form2(); form.Show(); Form2 privatevoid li
37、nkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) System.Diagnostics.Process ie = new System.Diagnostics.Process (); ie.StartInfo.FileName = IEXPLORE.EXE ;/ie浏览器 ie.StartInfo.Arguments = http:/;/链接 ie.Start(); 8个label 控件 ,1 个panel 控件,内容:姓名:张田新班级:网络工程1 班学号: 1225114042 邮箱: 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 10 页 - - - - - - - - -