资源描述
注意: 本程序采用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;
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
展开阅读全文