GDI+ Shapes: A Closed Curve
If you use either the DrawLines(), the DrawBezier() or the DrawBeziers() methods, you would get a continuous line or a series of lines that has a…
Introduction#
While methods like DrawLines(), DrawBezier(), and DrawBeziers() create lines with distinct start and end points, GDI+ also supports drawing shapes where the final point connects back to the starting one. You can achieve this using the Graphics.DrawClosedCurve() method, which offers four different overloads. Two of these are defined as follows:
public void DrawBeziers(Pen pen, Point[] points);
public void DrawClosedCurve(Pen pen, PointF[] points);These two versions are the easiest to use. They allow you to provide an array of four Point or four PointF values. When executed, each of these methods draws a curve that passes through each coordinate and closes the curve by joining the end point to the first unless both points are the same. Here is an example:
private void Exercise_Paint(object sender, PaintEventArgs e)
{
Pen penCurrent = new Pen(Color.Blue);
Point[] pt = { new Point(40, 42), new Point(188, 246),
new Point(484, 192), new Point(350, 48) };
e.Graphics.DrawClosedCurve(penCurrent, pt);
}This would produce:
The first two versions are used to draw the lines but curve them in order to make the shape appear smooth. If you want, you can draw the lines straight from one point to the next without curving them. Using this scenario, the above shape would appear as follows:
To draw this type of shape using the DrawClosedCurve() method, you can use one of the following versions of the method:
public void DrawClosedCurve(Pen pen,
Point[] points,
float tension,
FillMode fillmode);
public void DrawClosedCurve(Pen pen,
PointF[] points,
float tension,
FillMode fillmode);With these versions, you can adjust both the tension and the fill mode for the curve. The tension setting determines the degree of curvature; setting it to 0.00 results in straight lines between points, whereas higher values increase the curve. The fill mode, defined by the FillMode enumeration in System.Drawing.Drawing2D, specifies how the shape's interior is colored, using either the Alternate or Winding rules. For instance:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
Paint += new PaintEventHandler(Exercise_Paint);
}
private void Exercise_Paint(object sender, PaintEventArgs e)
{
Pen penCurrent = new Pen(Color.Red);
Point[] pt = { new Point(40, 42), new Point(188, 246),
new Point(484, 192), new Point(350, 48) };
e.Graphics.DrawClosedCurve(penCurrent, pt, 0.75F,
FillMode.Winding);
}
}
public class Program
{
public static int Main()
{
Application.Run(new Exercise());
return 0;
}
}This would produce:
Keep in mind that increasing the tension value will make the corners of the curve sharper. To render the shape with perfectly straight segments, simply use a tension of 0.00F. Home Copyright © 2010-2011 C# Key

