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

本文来源:https://www.bwwdw.com/article/wh98.html

Top