GraphicsPath对象

来源:互联网 发布:阿里云ecs 微信公众号 编辑:程序博客网 时间:2024/05/22 10:39
 

GraphicsPath对象


01 GraphicsPath对象
   它由一系列相互连接的直线、曲线连接起来组成的开放(非闭合)图形。
   创建路径时就会隐式创建一个新图形(由上面的直线、曲线等组成)。也可以
                 显示地声明StartFigure。
          
   图形具有方向,其先后顺序加入的直线、曲线等就表明了次序。


   一般图形路径是开放的,由起点,到最后图形(终点)。也可以用ClosedFigure显式

   声明为闭合图形(比如填充和剪辑时要用)


[vb] view plaincopyprint?
  1. '01.GraphicsPath图形路径,表示用一系列先后顺序的形状组成的图形  
  2. Imports System.Drawing  
  3. Imports System.Drawing.Drawing2D  
  4.   
  5. Public Class Form1  
  6.     Dim gr As Graphics = Me.CreateGraphics  
  7.     Dim gp As New GraphicsPath  
  8.     Dim pn As New Pen(Color.Red, 2)  
  9.   
  10.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  11.         gp.AddLine(New Point(0, 0), New Point(30, 20))  
  12.         gr.DrawPath(pn, gp)  
  13.     End Sub  
  14.   
  15.     Private Sub Button2_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button2.Click  
  16.         gp.AddEllipse(New Rectangle(20, 20, 20, 40))  
  17.         gr.DrawPath(pn, gp)  
  18.     End Sub  
  19.   
  20.     Private Sub Button3_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button3.Click  
  21.         gp.AddBezier(New Point(30, 60), New Point(70, 60), New Point(50, 30), New Point(100, 10))  
  22.         gr.DrawPath(pn, gp)  
  23.     End Sub  
  24.   
  25.     Private Sub Button4_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button4.Click  
  26.         gp.AddPie(New Rectangle(120, 0, 70, 70), 70, -90)  
  27.         gr.DrawPath(pn, gp)  
  28.     End Sub  
  29.   
  30.     Private Sub Button5_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button5.Click  
  31.         Dim pts As Point() = {New Point(10, 100), New Point(75, 10), New Point(80, 160), _  
  32.                             New Point(100, 150), New Point(125, 80), New Point(175, 200), _  
  33.                             New Point(200, 80)} '7个点(两个Bezier相连,4个控制点)  
  34.         gp.AddBeziers(pts)  
  35.         gr.DrawPath(pn, gp)  
  36.     End Sub  
  37. End Class  




02、路径

   GraphicsPath 允许将各种形状收集到一个单独的单元(如同集合一样)中。
   它用AddLine,AddCurve、AddClosedCurve、AddPie等来添加形状。

    而且路径还可添加到另一个路径中去,形成大型复杂路径。
           myGPath.AddPath(gp1,False)
   
    最后绘制路径:DrawPath(Pen,GP)


[vb] view plaincopyprint?
  1. '02 路径  
  2. Imports System.Drawing  
  3. Imports System.Drawing.Drawing2D  
  4. Public Class Form1  
  5.   
  6.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  7.         Dim gr As Graphics = Me.CreateGraphics  
  8.         Dim myPen As New Pen(Color.Blue, 2)  
  9.   
  10.         Dim gp As New GraphicsPath  
  11.         Dim points As Point() = {New Point(15, 20), New Point(20, 40), New Point(50, 30)}  
  12.         Dim sf As New StringFormat  
  13.   
  14.         gp.AddArc(New Rectangle(0, 0, 30, 20), -90, 180)  
  15.         gp.StartFigure() '不闭合当前图形即开始一个新图形。后面添加到该路径的所有点都被添加到此新图形中  
  16.   
  17.         gp.AddCurve(points)  
  18.         gp.AddString("字串添加入路径Path"New FontFamily("宋体"), FontStyle.Bold, 14, New PointF(50, 20), sf)  
  19.         gp.AddPie(New Rectangle(230, 10, 40, 40), 40, 110)  
  20.   
  21.         gr.DrawPath(myPen, gp)  
  22.     End Sub  
  23. End Class  



[vb] view plaincopyprint?
  1. '03 路径的开放与闭合  
  2. 'StartFigure 不须闭合,开始新图  
  3. 'CloseFigure 闭合当前图(可能是一个,也许是多个),开始新图。例子中闭合时机不同成图不同  
  4. Imports System.Drawing  
  5. Imports System.Drawing.Drawing2D  
  6. Public Class Form1  
  7.   
  8.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  9.         Dim gr As Graphics = Me.CreateGraphics  
  10.         Dim myPath As New GraphicsPath  
  11.   
  12.         ' First set of figures.  
  13.         myPath.StartFigure()  
  14.         myPath.AddArc(New Rectangle(10, 10, 50, 50), 0, 270)  
  15.         myPath.AddLine(New Point(50, 0), New Point(100, 50))  
  16.         myPath.AddArc(New Rectangle(50, 100, 75, 75), 0, 270)  
  17.         myPath.CloseFigure()  
  18.         myPath.StartFigure()  
  19.         myPath.AddArc(New Rectangle(100, 10, 50, 50), 0, 270)  
  20.   
  21.         ' Second set of figures.  
  22.         myPath.StartFigure()  
  23.         myPath.AddArc(New Rectangle(10, 200, 50, 50), 0, 270)  
  24.         myPath.CloseFigure()  
  25.         myPath.StartFigure()  
  26.         myPath.AddLine(New Point(60, 200), New Point(110, 250))  
  27.         myPath.AddArc(New Rectangle(50, 300, 75, 75), 0, 270)  
  28.         myPath.CloseFigure()  
  29.         myPath.StartFigure()  
  30.         myPath.AddArc(New Rectangle(100, 200, 50, 50), 0, 270)  
  31.   
  32.         ' Draw the path to the screen.  
  33.         gr.DrawPath(New Pen(Color.Black), myPath)  
  34.     End Sub  
  35. End Class  








3、GraphicsPath方法
   ClearMarkers   清除路径的所有标记
   SetMarkers     在GraphicsPath上设置标记


   CloseFigure    闭合当前图形,开始新图
   CloseAllFigure 闭合所有开放图形,开始新图


   Flatten        将此路径中的各段曲线转换成相连的线段序列
   GetBounds      返回限定此GraphicsPath对象的矩形(外沿)


   InitializeLifetimeService  获取控制此实例的生存期策略的生存期服务对象。
   IsVisible      指示指定点是否此GraphicsPath对象内


   IsOutlineVisible  指示当使用指定Pen对象绘制此GraphicsPath对象时,指定点
                     是否包含在后者的轮廓内(下)
   Reset          清空PathPoints和PathTypes数组并将FillMode设置为Alernate
   Reverse        反转GraphicsPath对象PathPoints数组各点顺序


   Transform      将变形矩形应用到此GraphicsPath对象
   Warp           对此GraphicsPath对象应用由一个矩形和一个平等四边形定义的扭曲变形
   Widen          在用指定的画笔绘制此路径时,用包含所填充区域的曲线代替此路径


[vb] view plaincopyprint?
  1. '04 Curve与ClosedCurve的对比  
  2. '弯曲程度(平滑与锐利的对比)  
  3.   
  4. Imports System.Drawing  
  5. Imports System.Drawing.Drawing2D  
  6. Public Class Form1  
  7.   
  8.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  9.         Dim gr As Graphics = Me.CreateGraphics  
  10.         Dim gp As New GraphicsPath  
  11.   
  12.         Dim p1 As Point() = {New Point(20, 100), New Point(40, 150), New Point(60, 125), _  
  13.                            New Point(40, 100), New Point(60, 75), New Point(40, 50)}  
  14.   
  15.         '下面对比封闭与开放  
  16.         gp.AddClosedCurve(p1, 0.5F) '1参:曲线上的点(Point数组),2参:曲线弯曲程序(0-1,1最平滑,0最锐利)  
  17.         gr.DrawPath(Pens.Red, gp)  
  18.   
  19.         gp.Reset() '清空gp中图形基元(相当于清空集合中元素)  
  20.         gr.TranslateTransform(50, 0) '向右平移坐标  
  21.         gp.AddCurve(p1, 0.5F)  
  22.         gr.DrawPath(Pens.Red, gp)  
  23.   
  24.         '下面对比一下弯曲的平滑程序  
  25.         gp.Reset()  
  26.         gr.TranslateTransform(50, 0) '在现有坐标上再右移50  
  27.         gp.AddCurve(p1, 0.1F)  
  28.         gr.DrawPath(Pens.Red, gp)  
  29.   
  30.         gp.Reset()  
  31.         gr.TranslateTransform(50, 0)  
  32.         gp.AddCurve(p1, 1.0F)  
  33.         gr.DrawPath(Pens.Red, gp)  
  34.     End Sub  
  35. End Class  



[vb] view plaincopyprint?
  1. '05 SetMarkers与ClearMarkers  
  2. '   GP.AddPath(otherPath,Connect)加入另一路径  
  3. '   DrawPath与FillPath  
  4. Imports System.Drawing  
  5. Imports System.Drawing.Drawing2D  
  6. Public Class Form1  
  7.   
  8.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  9.         Dim gr As Graphics = Me.CreateGraphics  
  10.         Dim gp As New GraphicsPath  
  11.         Dim gp2 As New GraphicsPath  
  12.   
  13.         '人为创建标志,用于分隔各部分基元,可用NextMarker来循环访问。  
  14.         '相当于分成小组,两个标记间可含有多个子路径  
  15.         gp.AddEllipse(New Rectangle(10, 10, 60, 100))  
  16.         gp.SetMarkers()  
  17.         gp.AddLine(New Point(70, 50), New Point(100, 50))  
  18.         gp.AddRectangle(New Rectangle(100, 10, 60, 100))  
  19.         gp.SetMarkers()  
  20.         gp.AddLine(New Point(130, 110), New Point(130, 140))  
  21.         gp.SetMarkers()  
  22.   
  23.         gp.ClearMarkers() '清除标记,小组解散。  
  24.         gr.DrawPath(Pens.Red, gp)  
  25.   
  26.   
  27.         Dim sf As New StringFormat  
  28.         gp2.AddString("只是文本轮廓"New FontFamily("楷体"), FontStyle.Italic, 20, New Point(33, 133), sf)  
  29.   
  30.         gp.AddPath(gp2, True'添加路径的第一个图形是否为该路径的最后图形的一部分  
  31.   
  32.         'gr有两种画法:DrawPath(画轮廓)、FillPath(填充)。  
  33.         '下面用的是DrawPath,所以画出的文字是轮廓(空心);而FillPath是实心文字  
  34.         gr.DrawPath(Pens.Red, gp)  
  35.     End Sub  
  36. End Class  





[vb] view plaincopyprint?
  1. '06.CloseFigure与CloseAllFigure对比  
  2. '   StartFigure不闭合开始新图  
  3. '   CloseFigure闭合前面图(起点和终点封闭),再开始新图  
  4. Imports System.Drawing.Drawing2D  
  5. Public Class Form1  
  6.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  7.         Dim gr As Graphics = Me.CreateGraphics  
  8.         Dim gp As New GraphicsPath  
  9.         Dim p As Point() = {New Point(120, 120), New Point(160, 128), _  
  10.                           New Point(160, 148), New Point(120, 160)}  
  11.   
  12.         gp.StartFigure() '不闭合,开始新图  
  13.         gp.AddLine(New Point(10, 10), New Point(150, 10))  
  14.         gp.AddLine(New Point(150, 10), New Point(10, 150))  
  15.   
  16.         gp.StartFigure()  
  17.         gp.AddArc(New Rectangle(70, 1, 100, 100), 0, 90)  
  18.   
  19.         gp.StartFigure()  
  20.         gp.AddCurve(p)  
  21.   
  22.         gr.DrawPath(Pens.Blue, gp)  
  23.   
  24.         '下面对比:闭合当前图形  
  25.         gr.TranslateTransform(190, 0)  
  26.         gp.CloseFigure() '闭合当前图形  
  27.         gr.DrawPath(Pens.Red, gp)  
  28.   
  29.         '下面对比:闭合所有图形  
  30.         gr.TranslateTransform(190, 0)  
  31.         gp.CloseAllFigures() '闭合所有图形  
  32.         gr.DrawPath(Pens.Black, gp)  
  33.     End Sub  
  34. End Class  







[vb] view plaincopyprint?
  1. '07.Flatten把曲线平展成多个连续的线段  
  2. Imports System.Drawing  
  3. Imports System.Drawing.Drawing2D  
  4. Public Class Form1  
  5.   
  6.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  7.         Dim gr As Graphics = Me.CreateGraphics  
  8.         Dim gp As New GraphicsPath  
  9.   
  10.         Dim p As Point() = {New Point(20, 100), New Point(70, 10), _  
  11.                           New Point(130, 200), New Point(180, 100)}  
  12.         Dim Matr As New Matrix  
  13.   
  14.         gp.AddCurve(p)  
  15.         gr.DrawPath(Pens.Black, gp)  
  16.   
  17.         '当前matr向下平移10  
  18.         Matr.Translate(0, 10)  
  19.         '指定曲线和其展平的近似直线之间的最大允许误差。值 0.25 是默认值。  
  20.         '降低该展平值将增加近似直线中线段的数目。(越小越接近曲线)  
  21.         gp.Flatten(Matr, 10.0F)  
  22.         gr.DrawPath(Pens.Red, gp)  
  23.   
  24.         '当前matr向下平移10  
  25.         Matr.Translate(0, 10)  
  26.         gp.Flatten(Matr, 1.0F)  
  27.         gr.DrawPath(Pens.Blue, gp)  
  28.   
  29.         '当前matr向下平移10  
  30.         Matr.Translate(0, 10)  
  31.         gp.Flatten(Matr, 200.0F)  
  32.         gr.DrawPath(Pens.Red, gp)  
  33.     End Sub  
  34. End Class  



[vb] view plaincopyprint?
  1. '08 GP.GetBounds 取得GP的边界(矩形区域,例:红色框中区域)  
  2. Imports System.Drawing.Drawing2D  
  3. Public Class Form1  
  4.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  5.         Dim gr As Graphics = Me.CreateGraphics  
  6.         Dim gp As New GraphicsPath  
  7.         Dim p As Point() = {New Point(120, 120), New Point(160, 128), _  
  8.                           New Point(160, 148), New Point(120, 160)}  
  9.   
  10.         gp.StartFigure() '不闭合,开始新图  
  11.         gp.AddLine(New Point(10, 10), New Point(150, 10))  
  12.         gp.AddLine(New Point(150, 10), New Point(10, 150))  
  13.   
  14.         gp.StartFigure()  
  15.         gp.AddArc(New Rectangle(70, 1, 100, 100), 0, 90)  
  16.   
  17.         gp.StartFigure()  
  18.         gp.AddCurve(p)  
  19.   
  20.         gr.DrawPath(Pens.Blue, gp)  
  21.         gr.DrawRectangle(Pens.Red, gp.GetBounds.Left, gp.GetBounds.Top, gp.GetBounds.Width, gp.GetBounds.Height)  
  22.     End Sub  
  23. End Class  



[vb] view plaincopyprint?
  1. '09 PathPoints、GetLastPoint   
  2. '   Pointcount 路径中点数  
  3. 'IsOutlineVisible 测试某点是否在图形的轮廓线内(上),是:True,否False  
  4. 'IsVisible  是否在GP对象内,与上面不同:上面是轮廓线上,这个是轮廓线所包围区域(不含轮廓线)  
  5. Imports System.Drawing.Drawing2D  
  6. Public Class Form1  
  7.   
  8.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  9.         Dim gr As Graphics = Me.CreateGraphics  
  10.         Dim gp As New GraphicsPath  
  11.   
  12.         gp.AddLine(New Point(10, 10), New Point(60, 60))  
  13.         MessageBox.Show(gp.GetLastPoint.X & "," & gp.GetLastPoint.Y) '60,40  
  14.         gp.AddEllipse(New Rectangle(0, 0, 100, 150))  
  15.         MessageBox.Show(gp.GetLastPoint.X & "," & gp.GetLastPoint.Y) '100,75  
  16.   
  17.         Dim a() As PointF = gp.PathPoints '路径中的点  
  18.         MessageBox.Show(a.GetUpperBound(0)) '14  (0-14)  
  19.         MessageBox.Show(gp.PointCount) '15  
  20.   
  21.         gr.DrawPath(Pens.Red, gp)  
  22.         MessageBox.Show(gp.IsOutlineVisible(New Point(50, 40), Pens.Black, gr)) 'False  
  23.   
  24.         gr.FillPath(Brushes.Blue, gp)  
  25.         MessageBox.Show(gp.IsOutlineVisible(New Point(50, 40), Pens.Black, gr)) 'False  
  26.         MessageBox.Show(gp.IsVisible(New Point(9, 9), gr))  
  27.     End Sub  
  28. End Class  



[vb] view plaincopyprint?
  1. '10.Transform 对GraphicsPaht对象进行变形(缩放、转换、旋转、扭曲)  
  2. '复合变换的顺序非常重要。一般说来,先旋转、再缩放、然后平移,  
  3. '与先缩放、再旋转、然后平移是不同的.  
  4. '所以这个顺序要注意  
  5. Imports System.Drawing.Drawing2D  
  6. Public Class Form1  
  7.   
  8.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  9.         Dim gr As Graphics = Me.CreateGraphics  
  10.         Dim gp As New GraphicsPath  
  11.         Dim matr As New Matrix  
  12.   
  13.         gp.AddRectangle(New Rectangle(10, 10, 100, 40))  
  14.         gr.DrawPath(Pens.Red, gp)  
  15.   
  16.         '由于Matrix是矩阵,变形就靠它,因矩阵太不利于直接操作,故Matrix提供了几个方法:  
  17.         'Matrix.Rotate(int32)   旋转  
  18.         'Matrix.Scale(1,2)    X,Y上的缩放  
  19.         'Matrix.Translate(x,y) 平移  
  20.         'Matrix.Shear(x,y)     x,y上的切变因子  
  21.   
  22.         '平移后旋转  
  23.         matr.Translate(130, 0)  
  24.         matr.Rotate(30)  
  25.   
  26.         gp.Transform(matr)  
  27.         gr.DrawPath(Pens.Black, gp)  
  28.   
  29.         '现有基础上,平移、再放大X轴=================  
  30.         matr.Translate(-250, 50)  
  31.         matr.Scale(2, 1)  
  32.   
  33.         gp.Transform(matr)  
  34.         gr.DrawPath(Pens.Black, gp)  
  35.     End Sub  
  36. End Class  



[vb] view plaincopyprint?
  1. '11.Warp 与前面Transform一样,进行变形  
  2. Imports System.Drawing.Drawing2D  
  3. Public Class Form1  
  4.   
  5.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  6.         Dim gr As Graphics = Me.CreateGraphics  
  7.         Dim gp As New GraphicsPath  
  8.         Dim matr As New Matrix  
  9.   
  10.         Dim srcRect As New Rectangle(30, 10, 50, 100)  
  11.         '目标平行四边行,可为三点(第四点由前面三点自动计算得出)  
  12.         Dim destPoints As PointF() = {New Point(120, 10), New Point(220, 35), New Point(130, 110)}  
  13.   
  14.         gp.AddRectangle(srcRect)  
  15.         gr.DrawPath(Pens.Black, gp) '画出原图  
  16.   
  17.         '下面变换  
  18.         matr.Translate(10, 0) '平移  
  19.         gp.Warp(destPoints, srcRect, matr, WarpMode.Perspective, 0.5F)  
  20.         gr.DrawPath(Pens.Red, gp)  
  21.     End Sub  
  22. End Class  



[vb] view plaincopyprint?
  1. '12 Widen缩放原GP上的轮廓粗细  
  2. ' widen(pen,matrix,flatness)  
  3. ' matrix用于变形  
  4. 'flatness 曲线展平程序  
  5. '如果希望填充线条之间的空间,必须使用FillPath,而不是DrawPath  
  6. Imports System.Drawing.Drawing2D  
  7. Public Class Form1  
  8.   
  9.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  10.         Dim gr As Graphics = Me.CreateGraphics  
  11.         Dim gp As New GraphicsPath  
  12.         Dim matr As New Matrix  
  13.   
  14.         gp.AddEllipse(0, 0, 100, 100)  
  15.         gp.AddEllipse(100, 0, 100, 100)  
  16.         gr.DrawPath(Pens.Black, gp)  
  17.   
  18.         Dim widenPen As New Pen(Color.Blue, 10) '此笔主要取宽度(轮廓加粗),颜色没用  
  19.         matr.Translate(50, 50) '平移  
  20.         gp.Widen(widenPen, matr, 1.0F) '开始变形  
  21.         gr.DrawPath(Pens.Red, gp)  '画出轮廓(注意:只有轮廓)  
  22.   
  23.         '清除,看FillPath效果  
  24.         gp.Reset()  
  25.         gp.AddEllipse(0, 0, 100, 100)  
  26.         gp.AddEllipse(100, 0, 100, 100)  
  27.         matr.Translate(-30, 50) '平移  
  28.         gp.Widen(widenPen, matr, 1.0F)  
  29.         gr.FillPath(Brushes.Red, gp) '填充轮廓(实心)  
  30.   
  31.         '再变换一次  
  32.         matr.Translate(250, -120) '平移  
  33.         gp.Widen(widenPen, matr, 1.0F) '开始变形  
  34.         gr.DrawPath(Pens.Red, gp)  '画出轮廓(注意:只有轮廓)  
  35.     End Sub  
  36. End Class  


4、轨迹梯度刷
   GraphicsPath按先后(轨迹)次序维护一系列线条和曲线。
   PathGradientBrush路径渐变刷,在中心点可定义颜色,边沿还可按轨迹分别指定颜色。


[vb] view plaincopyprint?
  1. '13.轨迹渐变 笔刷  
  2. '   笔刷不会超出指定的区域(例中指定的区域为红色矩形)  
  3.   
  4. Imports System.Drawing.Drawing2D  
  5. Public Class Form1  
  6.   
  7.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  8.         Dim gr As Graphics = Me.CreateGraphics  
  9.         Dim gp As New GraphicsPath  
  10.   
  11.         gp.AddEllipse(10, 10, 140, 70)  
  12.         Dim pb As New Drawing2D.PathGradientBrush(gp)  
  13.   
  14.         '中心色及边沿色  
  15.         Dim colors As Color() = {Color.FromArgb(255, 255, 255, 255)}  
  16.         pb.CenterColor = Color.FromArgb(255, 0, 0, 255)  
  17.         pb.SurroundColors = colors  
  18.   
  19.         gr.FillEllipse(pb, 10, 10, 140, 70)  
  20.   
  21.         '坐标平衡后,对指定区域的填充  
  22.         gr.TranslateTransform(170, 0)  
  23.         gr.FillRectangle(pb, 10, 10, 200, 40)  
  24.         gr.DrawRectangle(Pens.Red, 10, 10, 200, 40) '就是这个区域  
  25.     End Sub  
  26. End Class  



[vb] view plaincopyprint?
  1. '14 轨迹渐变刷的中心  
  2. '   中心点颜色及位置的设置  
  3. Imports System.Drawing.Drawing2D  
  4. Public Class Form1  
  5.   
  6.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  7.         Dim gr As Graphics = Me.CreateGraphics  
  8.         Dim gp As New GraphicsPath  
  9.   
  10.         gp.AddEllipse(30, 10, 140, 70)  
  11.         Dim pb As New PathGradientBrush(gp)  
  12.   
  13.         Dim colors As Color() = {Color.FromArgb(255, 0, 255, 255)}  
  14.         pb.CenterPoint = New Point(150, 40) '中心点  
  15.         pb.SurroundColors = colors          '四边颜色  
  16.   
  17.         gr.FillEllipse(pb, 30, 10, 140, 70)  
  18.   
  19.         '平移坐标后看中心颜色效果  
  20.         gr.TranslateTransform(170, 0)  
  21.         pb.CenterColor = Color.FromArgb(255, 0, 0, 255)  
  22.         gr.FillEllipse(pb, 30, 10, 140, 70)  
  23.     End Sub  
  24. End Class  



[vb] view plaincopyprint?
  1. '15.复习:不需要GraphicsPath,直接构造PathGradientBrush  
  2.   
  3. Imports System.Drawing.Drawing2D  
  4. Public Class Form1  
  5.   
  6.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  7.         Dim gr As Graphics = Me.CreateGraphics  
  8.         Dim points As Point() = {New Point(100, 0), New Point(200, 200), New Point(0, 200)}  
  9.         Dim colors As Color() = {Color.FromArgb(255, 0, 128, 0), Color.FromArgb(255, 255, 255, 255), _  
  10.                                Color.FromArgb(255, 0, 0, 255)}  
  11.         Dim pos As Single() = {0.0F, 0.4F, 1.0F}  
  12.   
  13.   
  14.         Dim pb As New PathGradientBrush(points)  
  15.         Dim cb As New ColorBlend '颜色混合,用于颜色与位置的插值  
  16.         cb.Colors = colors  
  17.         '值数组,指定沿渐变线距离的百分比  
  18.         '该数组的元素指定沿渐变线距离的百分比。例如,元素值 0.2f 指定该点距离起始点为总距离的 20%。  
  19.         '该数组中的元素由介于 0.0f 和 1.0f 之间的浮点值表示,并且数组的第一个元素必须是 0.0f,而最后一个元素必须是 1.0f。  
  20.         cb.Positions = pos  
  21.         pb.InterpolationColors = cb  
  22.   
  23.         gr.FillRectangle(pb, 0, 0, 200, 200) '此区域刚好容纳一个笔刷印,所以看上去只有一个三角形  
  24.         gr.DrawRectangle(Pens.Black, 0, 0, 200, 200)  
  25.     End Sub  
  26. End Class  



[vb] view plaincopyprint?
  1. '16.开放与闭合图形填充时,效果一样。  
  2. '   尽管开放图形没有闭合,但填充时却把它当作是闭合图形进行填充  
  3. Imports System.Drawing.Drawing2D  
  4. Public Class Form1  
  5.   
  6.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  7.         ' Create a GraphicsPath object.  
  8.         Dim myPath As New GraphicsPath  
  9.         Dim gr As Graphics = Me.CreateGraphics  
  10.         ' First set of figures.  
  11.         myPath.StartFigure()  
  12.         myPath.AddArc(10, 10, 50, 50, 0, 270)  
  13.         myPath.AddLine(New Point(50, 0), New Point(100, 50))  
  14.         myPath.AddArc(50, 100, 75, 75, 0, 270)  
  15.         myPath.CloseFigure()  
  16.         myPath.StartFigure()  
  17.         myPath.AddArc(100, 10, 50, 50, 0, 270)  
  18.         gr.DrawPath(New Pen(Color.Black), myPath)  
  19.   
  20.         '注释其中一句闭合,可以看到开放图形  
  21.         myPath.Reset()  
  22.         myPath.StartFigure()  
  23.         myPath.AddArc(10, 10, 50, 50, 0, 270)  
  24.         myPath.AddLine(New Point(50, 0), New Point(100, 50))  
  25.         myPath.AddArc(50, 100, 75, 75, 0, 270)  
  26.         'myPath.CloseFigure()  
  27.         myPath.StartFigure()  
  28.         myPath.AddArc(100, 10, 50, 50, 0, 270)  
  29.         gr.TranslateTransform(150, 0)  
  30.         gr.DrawPath(New Pen(Color.Black), myPath)  
  31.   
  32.         '填充开放图形  
  33.         gr.TranslateTransform(-150, 180)  
  34.         gr.FillPath(Brushes.Brown, myPath) '尽管是开放轮廓,但填充时却看着是闭合的  
  35.     End Sub  
  36. End Class 
0 0
原创粉丝点击