现在的位置: 首页 > 综合 > 正文

C#里面图形编程

2013年05月03日 ⁄ 综合 ⁄ 共 7610字 ⁄ 字号 评论关闭

像Java一样,C#提供了一整套相当丰富的类库、方法以及事件以供开发者使用。C#还引入了GDI+,它是由GDI演变而来的,具有比GDI更强大的功能而且简化了程序员的编程工作。所以开发者运用这些,就可以很方便的开发出具有强大图形图像功能的应用程序了。本文,笔者就通过一些实例像读者介绍一下C#中的图形编程的基本知识。

简单实例:

   首先,让我们从例子开始,以下是一个最简单的实例:

using System;
using System.Windows.Forms;
using System.Drawing;

public class Hello:Form {
public Hello() {
this.Paint += new PaintEventHandler(f1_paint);
}

private void f1_paint(object sender,PaintEventArgs e) {
Graphics g = e.Graphics;
g.DrawString("你好,C#!",new Font("Verdana",20),
new SolidBrush(Color.Tomato),40,40);
g.DrawRectangle(new Pen(Color.Pink,3),20,20,150,100);

}
public static void Main() {
Application.Run(new Hello());
}

}
   在上面的实例中,我们用到了一个方法:DrawString(),它带有5个参数。同时,我们发现在运用DrawString()方法以前,我们先创建了一个Graphics类型的对象g=e.Graphics,这就说明了在运用任何图形类的方法以前我们必须先创建该类的一个实例化对象。在DrawString()方法后,我们用到了DrawRectangle()方法,其实我们还可以运用其他的方法来画椭圆或是多边形等等。第一个实例还是相当简单易懂的,不是吗?

变换图形的度量单位:

   在图形编程中,默认的图形度量单位是象素。不过,你可以通过修改PageUnit属性来修改图形的度量单位,可以是英寸或是毫米等。实现方法如下:

Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch
操作颜色选择对话框:

   在实际运用特别是图形图像编程过程中,我们可能会经常碰到颜色选择对话框(以及下面要提到的字体选择对话框)。使用颜色选择对话框,我们可以让用户来选择系统预定的颜色以及用户自定义的颜色。在使用颜色选择对话框之前,我们必须先创建一个ColorDialog类型的对象:

ColorDialog cd = new ColorDialog();
   然后,我们就可以用ShowDialog()方法来显示颜色选择对话框了。之后,就可以通过调用用户的颜色选择进行相关的图形操作了。

   以下,我给大家一个实例。该实例中有一个按钮和一个文本框,通过点击按钮可以调出颜色选择对话框,根据用户的颜色选择就可以设置文本框的背景颜色了。

using System;
using System.Drawing;
using System.Windows.Forms;

public class Clr:Form{
Button b1 = new Button();
TextBox tb = new TextBox();
ColorDialog clg = new ColorDialog();

public Clr(){
b1.Click += new EventHandler(b1_click);
b1.Text = "选择颜色";
tb.Location = new Point(50,50);
this.Controls.Add(b1);
this.Controls.Add(tb);
}

public void b1_click(object sender, EventArgs e){
clg.ShowDialog();
tb.BackColor = clg.Color;
}

public static void Main() {
Application.Run(new Clr());
}

}
操作字体选择对话框:

   字体是图形编程的一个重要组成部分,通过设置不同的字体,你可以在程序中达到不同的视觉效果。和以上的颜色选择对话框的创建差不多,你可以很方便地创建一个字体选择对话框,并通过它来让用户选择其所需的字体。

   下面同样给出一个实例,这个实例和上面的实例差不多,只是用来字体选择对话框代替了原来的颜色选择对话框,最后是根据用户的字体选择来设置文本框的字体。

using System;
using System.Drawing;
using System.Windows.Forms;

public class Fonts:Form {
Button b1 = new Button();
TextBox tb = new TextBox();
FontDialog flg = new FontDialog();

public Fonts() {
b1.Click += new EventHandler(b1_click);
b1.Text = "选择字体";
tb.Location = new Point(50,50);

this.Controls.Add(b1);
this.Controls.Add(tb);
}

public void b1_click(object sender, EventArgs e) {
clg.ShowDialog();
tb.FontName = flg.Font;
}
public static void Main() {
Application.Run(new Fonts());
}

}
使用System.Drawing.Drawing2D名字空间:

   如果你有一些图形图像编程的经验,那么你一定知道画笔和画刷的概念。它们在图形编程有非常广泛的运用。System.Drawing.Drawing2D名字空间提供了相当强大的功能,能让开发者很容易地操作画笔以及画刷对象。比如,你可以通过设置画笔的DashStyle属性(有Dash、DashDot、Solid等风格)来确定直线的风格。同样,通过运用SolidBrush、HatchBrush、GradientBrush等画笔你可以很轻易地修改被填充区域的外观。比如,你可以用SolidBrush将一个矩形区域用许许多多不同粗细的直线来填充。那么,我们在什么时候运用画笔和画刷呢?就像上面的例子中那样,通常一个图形轮廓(运用DrawXXX()方法)是用画笔对象来实现的,而一个填充区域(运用FillXXX()方法)则是用画刷对象来实现的。

使用画笔对象:

   在下面的实例中,我们用到了System.Drawing.Drawing2D名字空间。实例中我们用画笔以不同的风格画了直线、椭圆、馅饼图形、多边形等图形。

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

public class Drawgra:Form {
public Drawgra() {
this.Text = "运用画笔示例";
this.Size = new Size(450,400);
this.Paint += new PaintEventHandler(Draw_Graphics);
}

public void Draw_Graphics(object sender,PaintEventArgs e) {
Graphics g = e.Graphics;
Pen penline = new Pen(Color.Red,5);
Pen penellipse = new Pen(Color.Blue,5);
Pen penpie = new Pen(Color.Tomato,3);
Pen penpolygon = new Pen(Color.Maroon,4);

/*DashStyle有Dash、DashDot、DashDotDot、Dot、Solid等风格*/
//以Dash风格画一条直线

penline.DashStyle = DashStyle.Dash;
g.DrawLine(penline,50,50,100,200);

//以DashDotDot风格画一个椭圆

penellipse.DashStyle = DashStyle.DashDotDot;
g.DrawEllipse(penellipse,15,15,50,50);

//以Dot风格画一个馅饼图形
penpie.DashStyle = DashStyle.Dot;
g.DrawPie(penpie,90,80,140,40,120,100);

//以Solid风格画一个多边形

g.DrawPolygon(penpolygon,new Point[]{
new Point(30,140),
new Point(270,250),
new Point(110,240),
new Point(200,170),
new Point(70,350),
new Point(50,200)});

}

public static void Main() {
Application.Run(new Drawgra());
}

}
使用画刷对象:

   画刷对象是用特定的颜色、模式或是图像来填充一块区域的。总共有四种类型的画刷:SolidBrush(默认的画刷)、HatchBrush、GradientBrush以及TexturedBrush。下面,我们分别给出实例来进行介绍。

   1、运用SolidBrush:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

public class Solidbru:Form {
public Solidbru() {
this.Text = "运用SolidBrush示例";
this.Paint += new PaintEventHandler(Fill_Graph);
}

public void Fill_Graph(object sender,PaintEventArgs e) {
Graphics g = e.Graphics;
//创建一把SolidBrush并用它来填充一个矩形区域
SolidBrush sb = new SolidBrush(Color.Pink);
g.FillRectangle(sb,50,50,150,150);

}

public static void Main() {
Application.Run(new Solidbru());
}

}

   2、运用HatchBrush:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
public class Hatchbru:Form {
public Hatchbru() {
this.Text = "运用HatchBrush示例";
this.Paint += new PaintEventHandler(Fill_Graph);
}

public void Fill_Graph(object sender,PaintEventArgs e) {
Graphics g = e.Graphics;
//创建一把HatchBrush并用它来填充一个矩形区域
/*该画刷的HatchStyle有DiagonalCross、
ForwardDiagonal、Horizontal、 Vertical、 Solid等不同风格 */
HatchStyle hs = HatchStyle.Cross;
HatchBrush sb = new HatchBrush(hs,Color.Blue,Color.Red);
g.FillRectangle(sb,50,50,150,150);
}

public static void Main() {
Application.Run(new Hatchbru());
}

}
   3、运用GradientBrush:

   GradientBrush又可分为LinearGradientBrush和PathGradientBrush两种,从它们的名称我们可以知道前者是线性渐变的,而后者则是路径渐变的,因而能创造出更复杂和完美的效果。下面我就给大家分别举例:

   1)、运用LinearGradientBrush:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

public class LinearGradientbru:Form {
public LinearGradientbru() {
this.Text = "运用LinearGradientBrush示例";
this.Paint += new PaintEventHandler(Fill_Graph);
}

public void Fill_Graph(object sender,PaintEventArgs e) {
Rectangle r = new Rectangle(500, 300, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow,
LinearGradientMode.BackwardDiagonal);
e.Graphics.FillRectangle(lb, r);
}

public static void Main() {
Application.Run(new LinearGradientbru());
}
}

   所得图形如下:

   2)、运用PathGradientBrush:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

public class PathGradientbru:Form {
public PathGradientbru() {
this.Text = "运用PathGradientBrush示例";
this.Paint += new PaintEventHandler(Fill_Graph);
}

public void Fill_Graph(object sender,PaintEventArgs e) {
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);

//先设置好一个路径
GraphicsPath path = new GraphicsPath(new Point[] {
new Point(40, 140),
new Point(275, 200),
new Point(105, 225),
new Point(190, 300),
new Point(50, 350),
new Point(20, 180),
}, new byte[] {

(byte)PathPointType.Start,
(byte)PathPointType.Bezier,
(byte)PathPointType.Bezier,
(byte)PathPointType.Bezier,
(byte)PathPointType.Line,
(byte)PathPointType.Line,
});

//创建一把PathGradientBrush

PathGradientBrush pgb = new PathGradientBrush(path);

//设置画刷的周围颜色

pgb.SurroundColors = new Color[] {
  Color.Green,
  Color.Yellow,
  Color.Red,
  Color.Blue,
  Color.Orange,
  Color.White,

};

//用画刷进行填充
e.Graphics.FillPath(pgb, path);
}

public static void Main() {
Application.Run(new PathGradientbru());
}

}
   所得图形如下:

   4、运用TexturedBrush:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

public Texturedbru() {

//创建一幅图像以供填充椭圆的背景用
Image bgimage = new Bitmap("dotnet.gif");
bgbrush = new TextureBrush(bgimage);
this.Paint+=new PaintEventHandler(Text_bru);

}

public void Text_bru(object sender,PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillEllipse(bgbrush,50,50,500,300);

}

public static void Main() {
Application.Run(new Texturedbru());
}
}
使用图像:

   图像在图形编程中经常要用到的,其对用户界面的作用也是非常明显的。在以前的编程过程中,对图像的操作细节是相当繁琐的而且很容易出错。现在,在GDI+下面,你可以用C#语言很容易的完成你所希望的图像编程。

   很简单,你只要通过以下步骤就可以实现图像的编程。

   1、 创建一个位图类对象如下:

    Image img = new Bitmap("image.bmp");

   2、 在DrawImage()方法中运用上述对象:

    g.DrawImage(img,20,20,100,90);

   至于使用图像的实例,限于篇幅,我就不再这里介绍了。相信大家能很容易地完成一个运用图像的实例的。

总结:

   在这篇文章中,我主要用到了两个非常核心的名字空间:一个是System.Drawing、一个是System.Drawing.Drawing2D。有了它们,我们就可以很方便的调用其中的方法、属性来实现以往进行图形编程需要付出很大代价才能完成的任务,这不能不说是GDI+给我们带来的优点。所以,掌握好GDI+,我相信你的图形编程能力会更上一层楼的。 

抱歉!评论已关闭.