注意:本程序采用vs2008版,不兼容vs2005。
如果觉得可以就收藏吧。
本程序仅供做c#实验的同学的一个参考。
画图程序设计实验目的:1.了解.net下多媒体编程技术。
2.掌握Graphics类绘制图像的方法。
3.掌握使用GDI+技术显示和保存图像的方法。
实验要求:1.设计一个画图程序。
2.可以绘制各种图形,可以选择画笔的线型和颜色。
3.可以将绘制的图像保存为位图文件。
Form1.cs//主程序代码using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;using System.Collections;using ;using .Sockets;using System.Threading;namespace WindowsApplication1{public enum LineStyle { SOLID, DASH, DOT};public partial class FrmDraw : Form{private Color PenColor;private enum Shape { LINE, ELLIPSE, RECTANGLE, CIRCLE, FillRectangle, Pencil, Eraser, ELLIPSE1 };private Shape DrawShape;private Point StartPoint;private Point EndPoint;private Point z;public Graphics gps;private bool MouseDownFlag;private Pen PenLine;private SolidBrush Solid;private Hashtable DrawObject;private int count;private string ShapeName;private LineStyle lineStyle;private bool isOpen;private string imagePath;private string strShape;private string strLineStyle;PaintEventArgs e1 = null;int i = 0;int j = 0;int x2 = -1, y2 = -1;public FrmDraw(){isOpen = false;count = 0;lineStyle = LineStyle.SOLID;MouseDownFlag = false;PenLine = new Pen(PenColor);PenColor = System.Drawing.Color.Black;DrawShape = Shape.LINE;DrawObject = new Hashtable();ShapeName = "LINE";strShape = "直线";strLineStyle = "实线";InitializeComponent();}//自定义颜色private void CoustumColor_Click(object sender, EventArgs e) {if (colorDialog.ShowDialog() == DialogResult.OK){PenColor = colorDialog.Color;}}//鼠标事件private void FrmDraw_MouseDown(object sender, MouseEventArgs e){MouseDownFlag = true;StartPoint.X = e.X;StartPoint.Y = e.Y;z.X = e.X;z.Y = e.Y;//Color clr = GetBkColor(new Point(e.X, e.Y));// MessageBox.Show("R" + clr.R.ToString() + "G" + clr.G.ToString() + "B" + clr.B.ToString());}private void FrmDraw_MouseMove(object sender, MouseEventArgs e){if (MouseDownFlag){Color BkColor = this.BackColor;//Color.FromArgb(212, 208, 200);if(DrawObject != null){foreach (DictionaryEntry de in DrawObject){string[] splipstr = de.Key.ToString().Split('|');switch (splipstr[0]){case"Pencil":((Pencil)de.Value).Draw(gps);break;case"LINE":((DrawLine)de.Value).Draw(gps);break;case"ELLIPSE":((DrawEelipse)de.Value).Draw(gps);break;case"RECTANGLE":((DrawRectangle)de.Value).Draw(gps);break;case"CIRCLE":((CIRCLE)de.Value).Draw(gps);break;}}}switch (lineStyle){case LineStyle.DASH:PenLine.DashStyle = DashStyle.Dash;break;case LineStyle.DOT:PenLine.DashStyle = DashStyle.Dot;break;case LineStyle.SOLID:PenLine.DashStyle = DashStyle.Solid;break;}switch (DrawShape){case Shape.Pencil:EndPoint = new Point(e.X, e.Y);PenLine.Color = PenColor;// gps.DrawLine(PenLine, z, EndPoint);Pencil pencil = new Pencil(z, EndPoint, PenLine, PenColor); DrawObject.Add(ShapeName + "|" + count+i, pencil);i++;break;case Shape.LINE:if (x2 != -1){PenLine.Color = BkColor;gps.DrawLine(PenLine, StartPoint, EndPoint);}EndPoint = new Point(e.X, e.Y);PenLine.Color = PenColor;gps.DrawLine(PenLine, StartPoint, EndPoint);break;case Shape.ELLIPSE:if (x2 != -1){PenLine.Color = BkColor;gps.DrawEllipse(PenLine, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,EndPoint.Y - StartPoint.Y);}EndPoint = new Point(e.X, e.Y);PenLine.Color = PenColor;gps.DrawEllipse(PenLine, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,EndPoint.Y - StartPoint.Y);break;case Shape.ELLIPSE1:if (x2 != -1){Solid.Color = BkColor;gps.FillEllipse(Solid, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,EndPoint.Y - StartPoint.Y);}EndPoint = new Point(e.X, e.Y);Solid=new SolidBrush( PenColor);gps.FillEllipse(Solid, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,EndPoint.Y - StartPoint.Y);break;case Shape.CIRCLE:if (x2 != -1){PenLine.Color = BkColor;gps.DrawEllipse(PenLine, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,EndPoint.X - StartPoint.X);}EndPoint = new Point(e.X, e.Y);PenLine.Color = PenColor;gps.DrawEllipse(PenLine, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,EndPoint.X - StartPoint.X);break;case Shape.RECTANGLE:/* float width = Math.Abs(e.X - StartPoint.X);//确定矩形的宽float heigth = Math.Abs(e.Y - StartPoint.Y);//确定矩形的高if (x2 != -1){PenLine.Color = Color.White;// gps.DrawRectangle(PenLine, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,// EndPoint.Y - StartPoint.Y);gps.DrawRectangle(PenLine, StartPoint.X, StartPoint.Y, width, heigth);}PenLine.Color = PenColor;if (e.X < StartPoint.X){StartPoint.X = e.X;}if (e.Y < StartPoint.Y){StartPoint.Y = e.Y;}gps.DrawRectangle(PenLine, StartPoint.X, StartPoint.Y, width, heigth); break;*/if (x2 != -1){PenLine.Color = BkColor;gps.DrawRectangle(PenLine, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,EndPoint.Y - StartPoint.Y);}EndPoint = new Point(e.X, e.Y);PenLine.Color = PenColor;gps.DrawRectangle(PenLine, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,EndPoint.Y - StartPoint.Y);break;case Shape.FillRectangle:if (x2 != -1){Solid.Color = BkColor;gps.FillRectangle(Solid, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,EndPoint.Y - StartPoint.Y);}EndPoint = new Point(e.X, e.Y);Solid=new SolidBrush(PenColor);gps.FillRectangle(Solid, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,EndPoint.Y - StartPoint.Y);break;}x2 = e.X;y2 = e.Y;z.X = e.X;z.Y = e.Y;}}private void FrmDraw_MouseUp(object sender, MouseEventArgs e){MouseDownFlag = false;count++;switch (DrawShape){case Shape.Pencil://Pencil pencil = new Pencil(StartPoint, EndPoint, PenLine, PenColor);//DrawObject.Add(ShapeName + "|" + count, pencil);break;case Shape.Eraser:;break;case Shape.ELLIPSE:DrawEelipse eelipse = new DrawEelipse(StartPoint, EndPoint, PenLine, PenColor, lineStyle);DrawObject.Add(ShapeName + "|" + count, eelipse);break;case Shape.CIRCLE:CIRCLE circle = new CIRCLE(StartPoint, EndPoint, PenLine, PenColor, lineStyle);DrawObject.Add(ShapeName + "|" + count, circle);break;case Shape.LINE:DrawLine line = new DrawLine(StartPoint, EndPoint, PenLine, PenColor, lineStyle);DrawObject.Add(ShapeName + "|" + count, line);break;case Shape.RECTANGLE:DrawRectangle rectangle = new DrawRectangle(StartPoint, EndPoint, PenLine, PenColor, lineStyle);DrawObject.Add(ShapeName + "|" + count, rectangle);break;}x2 = y2 = -1;if (e1 != null){FrmDraw_Paint(this, e1);}}//加载事件private void FrmDraw_Load(object sender, EventArgs e){gps = this.CreateGraphics();}private void toolLineStyle_SelectedIndexChanged(object sender, EventArgs e){switch (toolLineStyle.Text.Trim()){case"实线":lineStyle = LineStyle.SOLID;strLineStyle = "实线";break;case"虚线":lineStyle = LineStyle.DASH;strLineStyle = "虚线";break;case"点划线":lineStyle = LineStyle.DOT;strLineStyle = "点划线";break;}}private void ToolBtnLine_Click(object sender, EventArgs e){DrawShape = Shape.LINE;ShapeName = "LINE";strShape = "直线";}private void toolBtnEllipse_Click(object sender, EventArgs e){DrawShape = Shape.ELLIPSE;ShapeName = "ELLIPSE";strShape = "椭圆";}private void toolStripButton3_Click(object sender, EventArgs e){DrawShape = Shape.CIRCLE;ShapeName = "CIRCLE";strShape = "圆";}private void toolStripButton1_Click(object sender, EventArgs e){DrawShape = Shape.RECTANGLE;ShapeName = "RECTANGLE";strShape = "矩形";}private void toolStripButton4_Click(object sender, EventArgs e){DrawShape = Shape.Pencil;ShapeName = "Pencil";strShape = "铅笔";}//菜单栏事件private void退出ToolStripMenuItem_Click(object sender, EventArgs e) {this.Close();Application.Exit();}private void选项栏OToolStripMenuItem_Click(object sender, EventArgs e) {if (选项栏OToolStripMenuItem.Checked){选项栏OToolStripMenuItem.Checked = false;Option.Hide();}else{选项栏OToolStripMenuItem.Checked = true;Option.Show();}}private void工具箱ToolStripMenuItem_Click(object sender, EventArgs e) {if (工具箱ToolStripMenuItem.Checked){工具箱ToolStripMenuItem.Checked = false;ToolBox.Hide();}else{工具箱ToolStripMenuItem.Checked = true;ToolBox.Show();}}private void保存SToolStripMenuItem_Click(object sender, EventArgs e) {}private void新建NToolStripMenuItem_Click(object sender, EventArgs e) {gps = this.CreateGraphics();Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);gps.FillRectangle(new SolidBrush(this.BackColor), rect);DrawObject.Clear();}private void打开ToolStripMenuItem_Click(object sender, EventArgs e){openFileDialog.Filter = "JPEG File(*.jpg)|*.jpg|Bitmap File(*bmp)|*.bmp|All Files(*.*)|*.*";if (DialogResult.OK == openFileDialog.ShowDialog()){gps = this.CreateGraphics();Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);gps.FillRectangle(new SolidBrush(this.BackColor), rect);DrawObject.Clear();Image openBitmap = Image.FromFile(openFileDialog.FileName);gps.DrawImage(openBitmap, new Point(0, 0));isOpen = true;imagePath = openFileDialog.FileName;}}//重绘事件private void FrmDraw_Paint(object sender, PaintEventArgs e){this.e1 = e;if (DrawObject != null){if (isOpen){Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);gps.FillRectangle(new SolidBrush(this.BackColor), rect);Image openBitmap = Image.FromFile(imagePath);gps.DrawImage(openBitmap, new Point(0, 0));}foreach (DictionaryEntry de in DrawObject){string[] splipstr = de.Key.ToString().Split('|');switch (splipstr[0]){case"Pencil":((Pencil)de.Value).Draw(gps);break;case"LINE":((DrawLine)de.Value).Draw(gps);break;case"ELLIPSE":((DrawEelipse)de.Value).Draw(gps);break;case"CIRCLE":((CIRCLE)de.Value).Draw(gps);break;case"RECTANGLE":((DrawRectangle)de.Value).Draw(gps);break;}}}}//CIRCLE.cs圆using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;namespace WindowsApplication1{class CIRCLE{ private Point startPoint;private Point endPoint;private Color lineColor;private Color BkColor;private Pen linePen;private LineStyle lineStyle;public CIRCLE(Point start, Point end, Pen pen, Color clr, LineStyle style) {startPoint = start;endPoint = end;linePen = pen;lineColor = clr;lineStyle = style;BkColor = Color.FromArgb(227, 227, 227);}public void Draw(Graphics gps){linePen.Color = lineColor;switch (lineStyle){case LineStyle.DASH:linePen.DashStyle = DashStyle.Dash;break;case LineStyle.DOT:linePen.DashStyle = DashStyle.Dot;break;case LineStyle.SOLID:linePen.DashStyle = DashStyle.Solid;break;}gps.DrawEllipse(linePen, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.X - startPoint.X);}}}//DrawRectangle.csusing System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;namespace WindowsApplication1{class DrawRectangle{ private Point startPoint;private Point endPoint;private Color lineColor;private Color BkColor;private Pen linePen;private LineStyle lineStyle;public DrawRectangle(Point start, Point end, Pen pen, Color clr, LineStyle style){startPoint = start;endPoint = end;linePen = pen;lineColor = clr;lineStyle = style;BkColor = Color.FromArgb(227, 227, 227);}public void Draw(Graphics gps){linePen.Color = lineColor;switch (lineStyle){case LineStyle.DASH:linePen.DashStyle = DashStyle.Dash;break;case LineStyle.DOT:linePen.DashStyle = DashStyle.Dot;break;case LineStyle.SOLID:linePen.DashStyle = DashStyle.Solid;break;}gps.DrawRectangle(linePen, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);}}}//DrawEelipse.csusing System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;namespace WindowsApplication1{class DrawEelipse{private Point startPoint;private Point endPoint;private Color lineColor;private Color BkColor;private Pen linePen;private LineStyle lineStyle;public DrawEelipse(Point start, Point end, Pen pen, Color clr, LineStyle style){startPoint = start;endPoint = end;linePen = pen;lineColor = clr;lineStyle = style;BkColor = Color.FromArgb(227, 227, 227);}public void Draw(Graphics gps){linePen.Color = lineColor;switch (lineStyle){case LineStyle.DASH:linePen.DashStyle = DashStyle.Dash;break;case LineStyle.DOT:linePen.DashStyle = DashStyle.Dot;break;case LineStyle.SOLID:linePen.DashStyle = DashStyle.Solid;break;}gps.DrawEllipse(linePen, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);}}}C#编写仿windows画图程序,美容养颜吧制作:美容养颜吧制作。