The GraphicsPath Object

I l @ ve RuBoard

The GraphicsPath Object

GDI+ Graphics Paths are a convenient way of collecting together a number of graphical shapes , or their boundaries at least, into a single unit. A path , once created, can be manipulated in its entirety, filled, stroked , or used to perform other graphical operations, such as being used to create a clipping region.

Any combination of the following graphical primitives can be placed in a Path object:

  • Arcs

  • Bezier Splines

  • Cardinal splines

  • Ellipses

  • Lines

  • Paths

  • Pie segments

  • Polygons

  • Rectangles

  • Character Glyphs from Strings

The following code snippet produces the result seen in Figure 3.5.5.

 void OnPaint(object sender, PaintEventArgs e) {    GraphicsPath p=new GraphicsPath();    p.AddEllipse(10,10,100,100);    p.AddRectangle(new Rectangle(0,0,120,120));    Pen pen=new Pen(Color.Black,1);    e.Graphics.DrawPath(pen,p);    pen.Dispose(); } 
Figure 3.5.5. Two figures in a GraphicsPath .

graphics/0305fig05.gif

Filling the path by substituting a suitable brush and fill command produces the following effect (see Figure 3.5.6):

 SolidBrush brush = new SolidBrush(Color.Blue); e.Graphics.FillPath(brush,p); 
Figure 3.5.6. A filled path.

graphics/0305fig06.gif

The square part of the path has been filled and the circle has not. This is because the graphics FillMode for the path is set to the default setting, Alternate .

To fill everything inside the outermost boundary of the shape, set the FillMode to Winding .

Adding Text and Other Paths

One path can be added to another quite simply by calling the AddPath method. The following code snippet creates a second path and adds it to the one shown in Figure 3.5.5.

 void OnPaint(object sender, PaintEventArgs e) {    GraphicsPath p=new GraphicsPath();    p.AddEllipse(10,10,100,100);    p.AddRectangle(new Rectangle(0,0,120,120));    Pen pen=new Pen(Color.Black,1);    GraphicsPath p2=new GraphicsPath();    Point[] tripoint=new Point[3];    tripoint[0]=new Point(80,10);    tripoint[1]=new Point(80,110);    tripoint[2]=new Point(150,60);    p2.AddClosedCurve(tripoint,(float)0);    p2.AddPath(p,true);    SolidBrush brush = new SolidBrush(Color.Blue);    e.Graphics.FillPath(brush,p2);    e.Graphics.DrawPath(pen,p2); } 

Now the image displayed is the sum of the two paths. Figure 3.5.7 shows the output from the modified OnPaint handler.

Figure 3.5.7. The combination of two paths

graphics/0305fig07.gif

Placing text in a path is a great way of creating text effects. A text path only contains the glyph outlines of the letters used. These outlines can be used to create clip paths, filled with patterns or colors, scaled, rotated , or otherwise transformed to make some pretty impressive effects.

Modifying the code in our phantom OnPaint will illustrate again how this is accomplished.

 void OnPaint(object sender, PaintEventArgs e) {    GraphicsPath p=new GraphicsPath();    p.AddString("AYBABTU",FontFamily.GenericSansSerif,                     0,(float)72,new Point(0,0),                     StringFormat.GenericDefault);    SolidBrush brush = new SolidBrush(Color.Blue);    e.Graphics.FillPath(brush,p);    brush.Dispose(); } 

We'll continue this discussion in the next section because it leads nicely into that topic.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net