Drawing Bezier Curves in GDI+


This article has been excerpted from book "Graphics Programming with GDI+ ".

The Bezier curve, developed Pierre Bezier in the 1960s for CAD/CAM operations, has become one of the most used curves in drawing. A Bezier curve is defined by four points: two endpoints and two control points. Figure 3.24 shows an example of a Bezier curve in which A and B are the starting and ending points and C and D are two control points.

3.24.gif

FIGURE 3.24: A Bezier curve

The Graphics class provides the DrawBezier and DrawBeziers methods for drawing Bezier curves. DrawBezier draws a Bezier curve defined by four points: the starting point, two control points, and the ending point of the curve. The following example draws a Bezier curve with starting point (30,20), ending point (140,50), and control points (80,60) and (120,18).

e.Graphics.DrawBezier (bluePen, 30, 20, 80, 60, 120, 180, 140, 50);

DrawBeziers draws a series of Bezier curves from an array of Point structures. To draw multiple beziers, you need 3x+1 points, where x is the number of Bezier segments.

Listing 3.18 draws Bezier curves using both DrawBezier and DrawBeziers.

LISTING 3.18: Drawing Bezier curves


private
void Form1_Paint (object sender,
            System.Windows.Forms.PaintEventArgs e)
      {
            Graphics g = e.Graphics;

            // Create a pen
            Pen bluePen = new Pen (Color.Blue, 1);
            Pen redPen = new Pen (Color.Red, 1);

            // Create points for curve
            PointF pt1 = new PointF(40.0F, 50.0F);
            PointF pt2 = new PointF(50.0F, 75.0F);
            PointF pt3 = new PointF(100.0F, 115.0F);
            PointF pt4 = new PointF(200.0F, 180.0F);
            PointF pt5 = new PointF(200.0F, 150.0F);

           
PointF pt6 = new PointF(350.0F, 250.0F);
            PointF pt7 = new PointF(200.0F, 200.0F);

            PointF[] ptsArray =
            {
                  pt1, pt2, pt3, pt4, pt5, pt6, pt7
            };

            // Draw Bezier
            e.Graphics.DrawBezier
            (bluePen, 30, 20, 80, 60, 120, 180, 140, 50);

            // Draw Bezier
            e.Graphics.DrawBeziers (redPen, ptsArray);


           
// Dispose of object
            bluePen.Dispose();
            redPen.Dispose();
      }


Figure 3.25 shows the output from Listing 3.18

3.25.gif

FIGURE 3.25: Drawing Bezier curves

Conclusion


Hope the article would have helped you in understanding how to draw a Bezier Curve in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Up Next
    Ebook Download
    View all
    Learn
    View all