使用渐变画笔填充形状.docx

上传人:b****5 文档编号:6643364 上传时间:2023-01-08 格式:DOCX 页数:14 大小:154.88KB
下载 相关 举报
使用渐变画笔填充形状.docx_第1页
第1页 / 共14页
使用渐变画笔填充形状.docx_第2页
第2页 / 共14页
使用渐变画笔填充形状.docx_第3页
第3页 / 共14页
使用渐变画笔填充形状.docx_第4页
第4页 / 共14页
使用渐变画笔填充形状.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

使用渐变画笔填充形状.docx

《使用渐变画笔填充形状.docx》由会员分享,可在线阅读,更多相关《使用渐变画笔填充形状.docx(14页珍藏版)》请在冰豆网上搜索。

使用渐变画笔填充形状.docx

使用渐变画笔填充形状

使用渐变画笔填充形状

.NETFramework4.5

其他版本

可借助渐变画笔用渐变的颜色填充形状。

 例如,可借助水平渐变画笔,从形状的左边缘到右边缘用逐渐变化的颜色来填充形状。

 设想这样一个矩形:

它的左边缘为黑色(红色、绿色和蓝色分量均为0);右边为红色(这三个分量分别为255,0,0)。

 如果矩形的宽度为256个像素,则给定像素的红色分量将多于其左侧的像素的红色分量。

 在一行中,最左边像素的颜色分量为(0,0,0);第二个像素的分量为(1,0,0);第三个为(2,0,0),依此类推,直到到达最右边的像素,它的分量为(255,0,0)。

 这些插值颜色的值构成了颜色渐变。

当水平地、垂直地或平行一指定的斜线移动到时,线性渐变改变颜色。

 当在轨迹的内部和边界来回移动时,轨迹渐变改变颜色。

 可自定义轨迹渐变以获得多种效果。

下面的插图显示用线性渐变画笔填充矩形,用路径渐变画笔填充椭圆。

本节内容

如何:

创建线性渐变

演示如何使用 LinearGradientBrush 类创建线性渐变。

如何:

创建路径渐变

描述如何使用 PathGradientBrush 类创建路径渐变。

如何:

对渐变应用灰度校正

解释如何将灰度校正用于渐变画笔。

如何:

创建线性渐变

.NETFramework4.5

其他版本

GDI+提供水平、垂直和对角线方向线性渐变。

 在默认情况下,线性渐变中的颜色均匀地变化。

 当然,也可自定义线性渐变,使颜色非均匀变化。

下面的示例使用水平线性渐变画笔填充线条、椭圆和矩形。

LinearGradientBrush 构造函数接收四个参数:

两个点和两种颜色。

 第一个点(0,10)与第一种颜色(红色)相关联,第二个点(200,10)与第二种颜色(蓝色)相关联。

 正如您所期望的那样,从(0,10)绘制到(200,10)的线条的颜色从红色逐渐变成蓝色。

点(50,10)和点(200,10)中的10无关紧要。

 重要的是这两个点的第二个坐标相同——它们之间的连线是水平的。

 当水平坐标从0移到200时,椭圆和矩形也逐渐从红色变成蓝色。

下面的插图显示线条、椭圆和矩形。

 请注意,当水平坐标增加到200以上时,颜色渐变重复其自身。

使用水平线性渐变

∙将不透明红和不透明蓝分别作为第三和第四个参数传递。

C#

VB

DimlinGrBrushAsNewLinearGradientBrush(_

NewPoint(0,10),_

NewPoint(200,10),_

Color.FromArgb(255,255,0,0),_

Color.FromArgb(255,0,0,255))

DimpenAsNewPen(linGrBrush)

e.Graphics.DrawLine(pen,0,10,200,10)

e.Graphics.FillEllipse(linGrBrush,0,30,200,100)

e.Graphics.FillRectangle(linGrBrush,0,155,500,30)

 

在上面的示例中,当您从水平坐标0移到水平坐标200时,颜色分量成线性变化。

 例如,如果某个点的第一个坐标位于0和200的正中间,则其蓝色分量将是0和255正中间的值。

GDI+可用于调整颜色从渐变的一个边缘到另一个边缘变化的方式。

 假设您希望按照下表创建从黑色变到红色的渐变画笔。

水平坐标

RGB组件

0

(0,0,0)

40

(128,0,0)

200

(255,0,0)

请注意,当水平坐标才达到0到200之间的20%时,红色分量已达到一半亮度。

下面的示例设置 LinearGradientBrush 对象的 Blend 属性,以便使三个相对亮度与三个相对位置相关联。

 正如上表所示,相对亮度0.5与相对位置0.2相关联。

 该代码用渐变画笔填充椭圆和矩形。

下面的插图显示所得到的椭圆和矩形。

自定义线性渐变

∙将不透明黑和不透明红分别作为第三和第四个参数传递。

C#

VB

DimlinGrBrushAsNewLinearGradientBrush(_

NewPoint(0,10),_

NewPoint(200,10),_

Color.FromArgb(255,0,0,0),_

Color.FromArgb(255,255,0,0))

DimrelativeIntensitiesAsSingle()={0.0F,0.5F,1.0F}

DimrelativePositionsAsSingle()={0.0F,0.2F,1.0F}

'CreateaBlendobjectandassignittolinGrBrush.

DimblendAsNewBlend()

blend.Factors=relativeIntensities

blend.Positions=relativePositions

linGrBrush.Blend=blend

e.Graphics.FillEllipse(linGrBrush,0,30,200,100)

e.Graphics.FillRectangle(linGrBrush,0,155,500,30)

 

在上面的示例中,渐变的方向是水平的;即,当沿着水平线移动时,颜色逐渐变化。

 还可以定义垂直渐变和对角线渐变。

下面的示例将点(0,0)和点(200,100)传递给 LinearGradientBrush 构造函数。

 蓝色与(0,0)相关联,绿色与(200,100)相关联。

 用线性渐变画笔填充线条(画笔的宽度为10)和椭圆。

下面的插图显示该线条和椭圆。

 请注意,当沿着任何与穿过(0,0)和(200,100)的直线平行的直线移动时,椭圆中的颜色逐渐变化。

创建对角线性渐变

∙将不透明蓝和不透明绿分别作为第三和第四个参数传递。

C#

VB

DimlinGrBrushAsNewLinearGradientBrush(_

NewPoint(0,0),_

NewPoint(200,100),_

Color.FromArgb(255,0,0,255),_

Color.FromArgb(255,0,255,0))

'opaqueblue

'opaquegreen

DimpenAsNewPen(linGrBrush,10)

e.Graphics.DrawLine(pen,0,0,600,300)

e.Graphics.FillEllipse(linGrBrush,10,100,200,100)

 

如何:

创建路径渐变

.NETFramework4.5

其他版本

PathGradientBrush 类使您可以自定义用渐变色填充形状的方式。

 例如,可为轨迹的中心指定一种颜色;为轨迹的边界指定另一种颜色。

 还可为轨迹边界上的七个点分别指定颜色。

 说明

在GDI+中,路径是由 GraphicsPath 对象维护的一系列线条和曲线。

 有关GDI+路径的更多信息,请参见GDI+中的图形路径和构造并绘制轨迹。

使用路径渐变填充椭圆

∙下面的示例用路径渐变画笔填充椭圆。

 中心的颜色设置为蓝色;边界的颜色设置为浅绿色。

 下面的插图显示已填充的椭圆。

默认情况下,路径渐变画笔不会延伸到路径边界以外。

 如果您使用路径渐变画笔来填充延伸到路径边界以外的图形,则无法填充路径以外的屏幕区域。

下图演示在将以下代码中的 FillEllipse 调用更改为 e.Graphics.FillRectangle(pthGrBrush,0,10,200,40) 后所出现的情况。

C#

VB

'Createapaththatconsistsofasingleellipse.

DimpathAsNewGraphicsPath()

path.AddEllipse(0,0,140,70)

'Usethepathtoconstructabrush.

DimpthGrBrushAsNewPathGradientBrush(path)

'Setthecoloratthecenterofthepathtoblue.

pthGrBrush.CenterColor=Color.FromArgb(255,0,0,255)

'Setthecoloralongtheentireboundary

'ofthepathtoaqua.

DimcolorsAsColor()={Color.FromArgb(255,0,255,255)}

pthGrBrush.SurroundColors=colors

e.Graphics.FillEllipse(pthGrBrush,0,0,140,70)

 

前面的代码示例旨在用于Windows窗体,它需要 PaintEventHandler 的参数 PaintEventArgs e。

在边界上指定点

∙下面的示例由星形轨迹构造路径渐变画笔。

 该代码设置 CenterColor 属性,它将星形中心的颜色设置为红色。

 然后,该代码设置 SurroundColors 属性,以便在 points 数组中的各个点处指定不同的颜色(存储在 colors数组中)。

 最后一个代码语句用路径渐变画笔填充星形轨迹。

C#

VB

'Putthepointsofapolygoninanarray.

DimpointsAsPoint()={_

NewPoint(75,0),_

NewPoint(100,50),_

NewPoint(150,50),_

NewPoint(112,75),_

NewPoint(150,150),_

NewPoint(75,100),_

NewPoint(0,150),_

NewPoint(37,75),_

NewPoint(0,50),_

NewPoint(50,50)}

'Usethearrayofpointstoconstructapath.

DimpathAsNewGraphicsPath()

path.AddLines(points)

'Usethepathtoconstructapathgradientbrush.

DimpthGrBrushAsNewPathGradientBrush(path)

'Setthecoloratthecenterofthepathtored.

pthGrBrush.CenterColor=Color.FromArgb(255,255,0,0)

'Setthecolorsofthepointsinthearray.

DimcolorsAsColor()={_

Color.FromArgb(255,0,0,0),_

Color.FromArgb(255,0,255,0),_

Color.FromArgb(255,0,0,255),_

Color.FromArgb(255,255,255,255),_

Color.FromArgb(255,0,0,0),_

Color.FromArgb(255,0,255,0),_

Color.FromArgb(255,0,0,255),_

Color.FromArgb(255,255,255,255),_

Color.FromArgb(255,0,0,0),_

Color.FromArgb(255,0,255,0)}

pthGrBrush.SurroundColors=colors

'Fillthepathwiththepathgradientbrush.

e.Graphics.FillPath(pthGrBrush,path)

 

∙下面的示例在代码中不使用 GraphicsPath 对象而绘制一个路径渐变。

 在该示例中,特定的 PathGradientBrush 构造函数接收一系列点,但是不需要 GraphicsPath 对象。

 同时,请注意,PathGradientBrush 用于填充矩形而不是填充路径。

 矩形比用于定义画笔的闭合轨迹大,因此矩形的某些部分未由画笔涂色。

 下面的插图显示该矩形(虚线)以及该矩形被路径渐变画笔涂色的那部分。

C#

VB

'Constructapathgradientbrushbasedonanarrayofpoints.

DimptsFAsPointF()={_

NewPointF(0,0),_

NewPointF(160,0),_

NewPointF(160,200),_

NewPointF(80,150),_

NewPointF(0,200)}

DimpBrushAsNewPathGradientBrush(ptsF)

'Anarrayoffivepointswasusedtoconstructthepathgradient

'brush.Setthecolorofeachpointinthatarray.

'Point(0,0)isred

'Point(160,0)isgreen

'Point(160,200)isgreen

'Point(80,150)isblue

'Point(0,200)isred

DimcolorsAsColor()={_

Color.FromArgb(255,255,0,0),_

Color.FromArgb(255,0,255,0),_

Color.FromArgb(255,0,255,0),_

Color.FromArgb(255,0,0,255),_

Color.FromArgb(255,255,0,0)}

pBrush.SurroundColors=colors

'Setthecentercolortowhite.

pBrush.CenterColor=Color.White

'Usethepathgradientbrushtofillarectangle.

e.Graphics.FillRectangle(pBrush,NewRectangle(0,0,160,200))

 

自定义路径渐变

∙自定义路径渐变画笔的一种方法就是设置它的 FocusScales 属性。

 聚焦缩放指定位于主轨迹内部的内部轨迹。

 中心颜色显示在内部轨迹中的任何地方,而不是只显示在中心点。

下面的示例根据椭圆轨迹创建路径渐变画笔。

 该代码将边界颜色设置为蓝色,将中心颜色设置为浅绿色,然后使用路径渐变画笔填充椭圆轨迹。

接着,该代码设置路径渐变画笔的聚焦缩放。

 x聚焦缩放被设置为0.3,y聚焦缩放被设置为0.8。

 该代码调用 Graphics 对象的 TranslateTransform 方法,以便后来对 FillPath 的调用填充位于第一个椭圆右侧的椭圆。

若要观看聚焦缩放的效果,请设想一个与主椭圆共用一个中心的小椭圆。

 小(内部)椭圆是由主椭圆在水平方向上缩小0.3倍,在垂直方向上缩小0.8倍(围绕其中心)得到的。

 当从外部椭圆的边界移到内部椭圆的边界时,颜色逐渐从蓝色变成浅绿色。

 当从内部椭圆的边界移到共用中心时,颜色保持浅绿色。

下面的插图显示以下代码的输出。

 左边的椭圆只在中心点上为浅绿色。

 右边的椭圆在内部轨迹内部的任何地方都为浅绿色。

C#

VB

'Createapaththatconsistsofasingleellipse.

DimpathAsNewGraphicsPath()

path.AddEllipse(0,0,200,100)

'Createapathgradientbrushbasedontheellipticalpath.

DimpthGrBrushAsNewPathGradientBrush(path)

'Setthecoloralongtheentireboundarytoblue.

'Changedvariablenamefromcolor

DimblueColorAsColor()={Color.Blue}

pthGrBrush.SurroundColors=blueColor

'Setthecentercolortoaqua.

pthGrBrush.CenterColor=Color.Aqua

'Usethepathgradientbrushtofilltheellipse.

e.Graphics.FillPath(pthGrBrush,path)

'Setthefocusscalesforthepathgradientbrush.

pthGrBrush.FocusScales=NewPointF(0.3F,0.8F)

'Usethepathgradientbrushtofilltheellipseagain.

'Showthisfilledellipsetotherightofthefirstfilledellipse.

e.Graphics.TranslateTransform(220.0F,0.0F)

e.Graphics.FillPath(pthGrBrush,path)

使用插值自定义

∙自定义路径渐变画笔的另一种方法是指定插值颜色数组和插值位置数组。

下面的示例基于三角形创建路径渐变画笔。

 该代码设置路径渐变画笔的 InterpolationColors 属性,以便指定插值颜色数组(深绿色,浅绿色,蓝色)和插值位置数组(0,0.25,1)。

 当从三角形的边界移到中心点时,颜色将从深绿色逐渐变成浅绿色,然后从浅绿色变成蓝色。

 深绿色到浅绿色的转变发生在深绿色到蓝色转变的距离的25%处。

下面的插图显示用自定义路径渐变画笔填充的三角形。

C#

VB

'Verticesoftheoutertriangle

DimpointsAsPoint()={_

NewPoint(100,0),_

NewPoint(200,200),_

NewPoint(0,200)}

'NoGraphicsPathobjectiscreated.ThePathGradientBrush

'objectisconstructeddirectlyfromthearrayofpoints.

DimpthGrBrushAsNewPathGradientBrush(points)

'Createanarrayofcolorscontainingdarkgreen,aqua,andblue.

DimcolorsAsColor()={_

Color.FromArgb(255,0,128,0),_

Color.FromArgb(255,0,255,255),_

Color.FromArgb(255,0,0,255)}

'Darkgreenisattheboundaryofthetriangle.

'Aquais40percentofthewayfromtheboundarytothecenterpoint.

'Blueisatthecenterpoint.

DimrelativePositionsAsSingle()={_

0.0F,_

0.4F,_

1.0F}

DimcolorBlendAsNewColorBlend()

colorBlend.Colors=colors

colorBlend.Positions=relativePositions

pthGrBrush.InterpolationColors=colorBlend

'Fillarectanglethatislargerthanthetriangle

'specifiedinthePointarray.Theportionofthe

'rectangleoutsidethetrianglewillnotbepainted.

e.Graphics.FillRectangle(pthGrBrush,0,0,200,200)

 

设置中心点

∙在默认情况下,路径渐变画笔的中心点位于用来构造梯度刷的轨迹的形心。

 可通过设置 PathGradientBrush 类的 CenterPoint 属性更改中心点的位置。

下面的示例基于椭圆来创建路径渐变画笔。

 椭圆的中心位于(70,35),但是路径渐变画笔的中心点设置在(120,40)。

C#

VB

'Createapaththatconsistsofasingleellipse.

DimpathAsNewGraphicsPath()

path.AddEllipse(0,0,140,70)

'Usethepathtoconstructabrush.

DimpthGrBrushAsNewPathGradientBrush(path)

'Setthecenterpointtoalocationthatisnot

'thecentroidofthepath.

pthGrBrush.CenterPoint=NewPointF(120,40)

'Setthecoloratthecenterofthepathtoblue.

pthGrBrush.CenterColor=Color.FromArgb(255,0,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 电力水利

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1