打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C#.net 地图控件开发(五) GDI+图形图像编程基础3.

画刷和画刷类型

Brush类型是一个抽象类,所以它不能被实例化,也就是不能直接应用,但是我们可以利用它的派生类,如:HatchBrush、SolidBrush、TextureBrush、LinearGradientBrush和PathGradientBrush等。画刷类型一般在System.Drawing命名空间中,如果应用HatchBrush和GradientBrush画刷,需要在程序中引入System.Drawing.Drawing2D命名空间。

 

1. SolidBrush(单色画刷)

它是一种一般的画刷,通常只用一种颜色去填充GDI+图形,例如:

protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

SolidBrush sdBrush1 = new SolidBrush(Color.Red);

SolidBrush sdBrush2 = new SolidBrush(Color.Green);      
SolidBrush sdBrush3 = new SolidBrush(Color.Blue);

g.FillEllipse(sdBrush2, 20, 40, 60, 70);

Rectangle rect = new Rectangle(0, 0, 200, 100);

g.FillPie(sdBrush3, 0, 0, 200, 40, 0.0f, 30.0f );

PointF point1 = new PointF(50.0f, 250.0f);
PointF point2 = new PointF(100.0f, 25.0f);

PointF point3 = new PointF(150.0f, 40.0f);

PointF point4 = new PointF(250.0f, 50.0f);

PointF point5 = new PointF(300.0f, 100.0f);

PointF[] curvePoints = {point1, point2, point3, point4,point5 };

g.FillPolygon(sdBrush1, curvePoints);

}

 

2. HatchBrush(阴影画刷)

HatchBrush类位于System.Drawing.Drawing2D命名空间中。阴影画刷有两种颜色:前景色和背景色,以及6种阴影。前景色定义线条的颜色,背景色定各线条之间间隙的颜色。HatchBrush类有两个构造函数:

        public HatchBrush(HatchStyle,Color forecolor);

        public HatchBrush(HatchStyle,Color forecolor,Color backcolor);

HatchStyle枚举值指定可用于HatchBrush对象的不同图案。

HatchStyle的主要成员如下表所示。

HatchStyle主要成员

名称

说明

BackwardDiagonal

从右上到左下的对角线的线条图案。

Cross

指定交叉的水平线和垂直线。

DarkDownwardDiagonal

指定从顶点到底点向右倾斜的对角线,其两边夹角比ForwardDiagonal小50%,宽度是其两倍。此阴影图案不是锯齿消除的。

DarkHorizontal

指定水平线的两边夹角比Horizontal小50%并且宽度是Horizontal的两倍。

DarkUpwardDiagonal

指定从顶点到底点向左倾斜的对角线,其两边夹角比BackwardDiagonal小50%,宽度是其两倍,但这些直线不是锯齿消除的。

DarkVertical

指定垂直线的两边夹角比Vertical小50%并且宽度是其两倍。

DashedDownwardDiagonal

指定虚线对角线,这些对角线从顶点到底点向右倾斜。

DashedHorizontal

指定虚线水平线。

DashedUpwardDiagonal

指定虚线对角线,这些对角线从顶点到底点向左倾斜。

DashedVertical

指定虚线垂直线。

DiagonalBrick

指定具有分层砖块外观的阴影,它从顶点到底点向左倾斜。

DiagonalCross

交叉对角线的图案。

Divot

指定具有草皮层外观的阴影。

ForwardDiagonal

从左上到右下的对角线的线条图案。

Horizontal

水平线的图案。

HorizontalBrick

指定具有水平分层砖块外观的阴影。

LargeGrid

指定阴影样式Cross。

LightHorizontal

指定水平线,其两边夹角比Horizontal小50%。

LightVertical

指定垂直线的两边夹角比Vertical小50%。

Max

指定阴影样式SolidDiamond。

Min

指定阴影样式Horizontal。

NarrowHorizontal

指定水平线的两边夹角比阴影样式Horizontal小 75%(或者比LightHorizontal小25%)。

NarrowVertical

指定垂直线的两边夹角比阴影样式Vertical小 75%(或者比LightVertica小25%)。

OutlinedDiamond

指定互相交叉的正向对角线和反向对角线,但这些对角线不是锯齿消除的。

Percent05

指定5%阴影。前景色与背景色的比例为5:100。

Percent90

指定90%阴影。前景色与背景色的比例为90:100。

Plaid

指定具有格子花呢材料外观的阴影。

Shingle

指定带有对角分层鹅卵石外观的阴影,它从顶点到底点向右倾斜。

SmallCheckerBoard

指定带有棋盘外观的阴影。

SmallConfetti

指定带有五彩纸屑外观的阴影。

SolidDiamond

指定具有对角放置的棋盘外观的阴影。

Sphere

指定具有球体彼此相邻放置的外观的阴影。

Trellis

指定具有格架外观的阴影。

Vertical

垂直线的图案。

Wave

指定由代字号“~”构成的水平线。

Weave

指定具有织物外观的阴影。

下面代码显示了HatchBrush画刷的使用。

protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

HatchBrush hBrush1 = new HatchBrush(HatchStyle.DiagonalCross, Color.Chocolate, Color.Red);

HatchBrush hBrush2 = new HatchBrush(HatchStyle.DashedHorizontal,

Color.Green, Color.Black);

HatchBrush hBrush3 = new HatchBrush(HatchStyle.Weave,

Color.BlueViolet, Color.Blue);

g.FillEllipse(hBrush1, 20, 80, 60, 20);

Rectangle rect = new Rectangle(0, 0, 200, 100);

g.FillPie(hBrush3, 0, 0, 200, 40, 0.0f, 30.0f );

PointF point1 = new PointF(50.0f, 250.0f);                      

PointF point2 = new PointF(100.0f, 25.0f);

PointF point3 = new PointF(150.0f, 40.0f);

PointF point4 = new PointF(250.0f, 50.0f);

PointF point5 = new PointF(300.0f, 100.0f);

PointF[] curvePoints = {point1, point2, point3, point4, point5 };

g.FillPolygon(hBrush2, curvePoints);

}

 

3. TextureBrush(纹理画刷)

纹理画刷拥有图案,并且通常使用它来填充封闭的图形。为了对它初始化,可以使用一个已经存在的别人设计好了的图案,或使用常用的设计程序设计的自己的图案,同时应该使图案存储为常用图形文件格式,如BMP格式文件。这里有一个设计好的位图,被存储为Papers.bmp文件。

private void Form1_Paint(object sender, PaintEventArgs e)

{        

Graphics g = e.Graphics;

//根据文件名创建原始大小的bitmap对象

Bitmap bitmap = new Bitmap("D:\\mm.jpg");

//将其缩放到当前窗体大小

bitmap = new Bitmap(bitmap, this.ClientRectangle.Size);

TextureBrush myBrush = new TextureBrush(bitmap);

g.FillEllipse(myBrush, this.ClientRectangle);

}

 

4.LinearGradientBrush和PathGradientBrush(渐变画刷)

渐变画刷类似与实心画刷,因为它也是基于颜色的,与实心画刷不同的是:渐变画刷使用两种颜色;它的主要特点是:在使用过程中,一种颜色在一端,而另外一种颜色在另一端,在中间,两种颜色融合产生过渡或衰减的效果。

渐变画刷有两种:线性画刷和路径画刷(LinearGradientBrush和PathGradientBrush)。

其中LinearGradientBrush可以显示线性渐变效果,而PathGradientBrush是路径渐变的可以显示比较具有弹性的渐变效果。

(1)LinearGradientBrush类

LinearGradientBrush类构造函数如下:

public LinearGradientBrush(Point point1,Point point2,Color color1,Color color2)

参数说明:

point1:表示线性渐变起始点的Point结构。

point2:表示线性渐变终结点的Point结构。

color1:表示线性渐变起始色的Color结构。

color2:表示线性渐变结束色的Color结构。

代码如下:

private void Form1_Paint(object sender, PaintEventArgs e)

{

Graphics g = e.Graphics;

LinearGradientBrush myBrush = new LinearGradientBrush(this.ClientRectangle, Color.White, Color.Blue, LinearGradientMode.Vertical);

g.FillRectangle(myBrush, this.ClientRectangle);

}

 

(2)PathGradientBrush类

PathGradientBrush类的构造函数如下:

public PathGradientBrush (GraphicsPath path);

参数说明:

path:GraphicsPath,定义此PathGradientBrush填充的区域。

例子代码如下:

private void Form1_Paint(object sender, PaintEventArgs e)

{

Graphics g = e.Graphics;

Point centerPoint = new Point(150, 100);

int R = 60;

GraphicsPath path = new GraphicsPath();

path.AddEllipse(centerPoint.X-R,centerPoint.Y-R,2*R,2*R);

PathGradientBrush brush = new PathGradientBrush(path);

 //指定路径中心点

 brush.CenterPoint = centerPoint;

 //指定路径中心的颜色

brush.CenterColor = Color.Red;

//Color类型的数组指定与路径上每个顶点的颜色

brush.SurroundColors = new Color[] { Color.Plum };

g.FillEllipse(brush,centerPoint.X-R,centerPoint.Y-R,2*R,2* R);

centerPoint = new Point(350, 100);

R = 20;

path = new GraphicsPath();

path.AddEllipse(centerPoint.X-R,centerPoint.Y-R,2*R,2*R);

path.AddEllipse(centerPoint.X-2*R,centerPoint.Y-2*R,4*R,4* R);

path.AddEllipse(centerPoint.X-3*R,centerPoint.Y-3*R,6*R,6* R);

brush = new PathGradientBrush(path);

brush.CenterPoint = centerPoint;

brush.CenterColor = Color.Red;

brush.SurroundColors = new Color[] { Color.Black, Color.Blue, Color.Green };

g.FillPath(brush, path);

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
GDI C# 画图 、 输出文字
笔记一:画笔、笔刷认识
VA19.1 GDI plus绘图基础
C# Graphics 绘图(及Color、Pen、Brush)
GDI+中使用画刷
用C#实现k均值聚类算法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服