c#仿windows画图程序完整版
更新时间:2024-03-11 00:04:01 阅读量: 综合文库 文档下载
注意: 本程序采用vs2008版,不兼容vs2005。
如果觉得可以就收藏吧。本程序仅供做c#实验的同学的一个参考。画图程序设计 实验目的:
1.了解.net下多媒体编程技术。
2.掌握Graphics类绘制图像的方法。
3.掌握使用GDI+技术显示和保存图像的方法。 实验要求:
1.设计一个画图程序。
2.可以绘制各种图形,可以选择画笔的线型和颜色。 3.可以将绘制的图像保存为位图文件。
Form1.cs
//主程序代码
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;
using System.Text;
using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Collections; using System.Net; using System.Net.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;
1
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 = \; strShape = \直线\; strLineStyle = \实线\; InitializeComponent(); }
//自定义颜色
private void CoustumColor_Click(object sender, EventArgs e) {
if (colorDialog.ShowDialog() == DialogResult.OK) {
PenColor = colorDialog.Color; } }
2
//鼠标事件
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(\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)de.Value).Draw(gps); break; case \:
((DrawLine)de.Value).Draw(gps); break; case \:
((DrawEelipse)de.Value).Draw(gps); break; case \:
((DrawRectangle)de.Value).Draw(gps); break; case \:
((CIRCLE)de.Value).Draw(gps);
3
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;
4
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); {
Solid.Color = BkColor;
gps.FillEllipse(Solid, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,
EndPoint.Y - StartPoint.Y); }
EndPoint = Solid= gps.FillEllipse(Solid, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,
EndPoint.Y - StartPoint.Y); {
PenLine.Color = BkColor;
gps.DrawEllipse(PenLine, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,
EndPoint.X - StartPoint.X); }
EndPoint = PenLine.Color = PenColor;
gps.DrawEllipse(PenLine, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,
EndPoint.X - StartPoint.X);
break;
case Shape.ELLIPSE1: if (x2 != -1) new Point(e.X, e.Y); new SolidBrush( PenColor);
break; case Shape.CIRCLE: if (x2 != -1) new Point(e.X, e.Y); break; case Shape.RECTANGLE:
5
/* 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, break;*/
{
PenLine.Color = BkColor;
gps.DrawRectangle(PenLine, StartPoint.X, - StartPoint.X,
EndPoint.Y - StartPoint.Y); }
EndPoint = PenLine.Color = PenColor;
gps.DrawRectangle(PenLine, StartPoint.X, StartPoint.Y, EndPoint.X - StartPoint.X,
EndPoint.Y - StartPoint.Y); {
Solid.Color = BkColor;
StartPoint.X, StartPoint.Y, width, heigth); if (x2 != -1) StartPoint.Y, EndPoint.X new Point(e.X, e.Y); break;
case Shape.FillRectangle: if (x2 != -1) 6
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 + \ 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;
7
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 = \点划线\;
8
break; } }
private void ToolBtnLine_Click(object sender, EventArgs e) {
DrawShape = Shape.LINE; ShapeName = \; strShape = \直线\; }
private void toolBtnEllipse_Click(object sender, EventArgs e) {
DrawShape = Shape.ELLIPSE; ShapeName = \; strShape = \椭圆\; }
private void toolStripButton3_Click(object sender, EventArgs e) {
DrawShape = Shape.CIRCLE; ShapeName = \; strShape = \圆\; }
private void toolStripButton1_Click(object sender, EventArgs e) {
DrawShape = Shape.RECTANGLE; ShapeName = \; strShape = \矩形\; }
private void toolStripButton4_Click(object sender, EventArgs e) {
DrawShape = Shape.Pencil; ShapeName = \; strShape = \铅笔\; }
//菜单栏事件
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) {
this.Close(); Application.Exit(); }
9
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();
10
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) {
openFileDialog.Filter = \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)de.Value).Draw(gps); break; case \:
((DrawLine)de.Value).Draw(gps);
11
break; case \:
((DrawEelipse)de.Value).Draw(gps); break; case \:
((CIRCLE)de.Value).Draw(gps); break;
case \:
((DrawRectangle)de.Value).Draw(gps); break;
}
} } }
//CIRCLE.cs圆
using System;
using System.Collections.Generic; using System.ComponentModel; 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;
12
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.cs
using System;
using System.Collections.Generic; using System.ComponentModel; 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;
13
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.cs
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;
using System.Text;
using System.Windows.Forms; using System.Drawing.Drawing2D;
namespace WindowsApplication1 {
14
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画图程序,美容养颜吧制作:http://www.mryyb.com 美容养颜吧制作。
//DrawLine.cs
using System;
15
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;
using System.Text;
using System.Windows.Forms; using System.Drawing.Drawing2D;
namespace WindowsApplication1 {
class DrawLine {
private Point startPoint; private Point endPoint;
private Color lineColor; private Color BkColor; private Pen linePen; private LineStyle lineStyle;
public DrawLine(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;
16
}
gps.DrawLine(linePen, startPoint, endPoint); } } }
//Pencil.cs
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;
using System.Text;
using System.Windows.Forms; using System.Drawing.Drawing2D; namespace WindowsApplication1 {
class Pencil {
private Point startPoint; private Point endPoint;
private Color lineColor;
private Pen linePen;
public Pencil(Point start, Point end, Pen pen, Color clr) {
startPoint = start; endPoint = end; linePen = pen; lineColor = clr; }
public void Draw(Graphics gps) {
linePen.Color = lineColor;
gps.DrawLine(linePen, startPoint, endPoint); } }
17
}
18
正在阅读:
c#仿windows画图程序完整版03-11
华为BRAS常用命令解释06-12
重庆周边乡镇赶场时间表01-10
浅析资源型城市的可持续性发展06-02
外国文学史欧美卷 复习资料11-16
2014中考复习《二次函数》精01-05
党支部书记个人述职述廉报告09-07
税收岗位2022年个人工作总结05-02
湖北2012年高考英语完成句子分析与讲解10-08
华为交换机视频类故障案例集(v1.0)04-04
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- c#
- 整版
- 画图
- windows
- 程序
- 小班幼儿发展评估总结(五大领域评估)
- 2016年五道口考研参考书及复试真题
- 糖友发生低血糖如何自救?
- 中央政法委通报16起政法干警违纪违法典型案件
- 工程训练总结报告
- 2018.1怀柔区初三化学期末试卷及答案
- 人教版小学语文三年级上册《我们的民族小学》课堂实录
- 20109-2012年度工作总结1
- XX年4月网页设计专业大学生实习报告
- 食堂管理制度
- 弧圈球打法乒乓球底板和套胶的选择
- 2018反邪教知识竞赛试题和答案解析
- 历年房地产宏观调控政策一览
- 华制培训课程计划表(公开课、标杆企业考察)2.1
- 房地产销售培训资料
- 建筑工程的分部工程、分项工程划分(gb50300-2013)未加细化版 -
- 网友经历
- 专题带电粒子在交变磁场中运动问题-2018年高考物理100考点最新模
- 鄄城职教中心语文精选试题
- 第二章 实验部分