A GraphicsPath is merely a collection of lines and curves that can be accessed through the GraphicsPath rather than individually.
The following code is the event handler for the Paint event. This handler can be dropped into any WinForm Form application. When the event is raised, the handler performs two major actions.
- A rectangle is declared, drawn, and filled; a triangle is declared, drawn, and filled. The drawing and filling is of the shapes themselves.
- A GraphicsPath is declared; a rectangle is declared and added to the GraphicsPath; a triangle is declared and added to the GraphicsPath (both the rectangle and the triangle are offset 150 pixels downward); the GraphicsPath is drawn and filled.
protected override void OnPaint ( PaintEventArgs e )
Point [ ] triangle = new Point [ 4 ];
base.OnPaint ( e );
this.Width = 200;
this.Height = 300;
rectangle = new Rectangle ( 10, 10, 100, 100 );
e.Graphics.DrawRectangle ( Pens.Red, rectangle );
e.Graphics.FillRectangle ( Brushes.Red, rectangle );
triangle [ 0 ] = new Point ( 120, 60 );
triangle [ 1 ] = new Point ( 180, 10 );
triangle [ 2 ] = new Point ( 180, 110 );
triangle [ 3 ] = new Point ( 120, 60 );
e.Graphics.DrawLines ( Pens.Red,triangle );
e.Graphics.FillPolygon ( Brushes.Red, triangle );
graphics_path = new GraphicsPath ( );
rectangle = new Rectangle ( 10, 160, 100, 100 );
graphics_path.AddRectangle ( rectangle );
triangle [ 0 ] = new Point ( 120, 210 );
triangle [ 1 ] = new Point ( 180, 160 );
triangle [ 2 ] = new Point ( 180, 260 );
triangle [ 3 ] = new Point ( 120, 210 );
graphics_path.AddLines ( triangle );
e.Graphics.DrawPath ( Pens.Blue, graphics_path );
e.Graphics.FillPath ( Brushes.Blue, graphics_path );
graphics_path.Dispose ( );
GraphicsPaths are somewhat easier to use than Regions. Remember to Dispose of the GraphicsPath when done with it.
Hope that helps.