使用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());
}
}
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());
}
}
