Drawing Bezier Curves in GDI+ using VB.NET

In this article I will explain you how to draw a Bezier Curve in GDI+.
  • 4116

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.


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

Public Class Form1

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics

        ' Create a pen
        Dim bluePen As New Pen(Color.Blue, 1)
        Dim redPen As New Pen(Color.Red, 1)
        ' Create points for curve
        Dim pt1 As New PointF(40.0F, 50.0F)
        Dim pt2 As New PointF(50.0F, 75.0F)
        Dim pt3 As New PointF(100.0F, 115.0F)
        Dim pt4 As New PointF(200.0F, 180.0F)
        Dim pt5 As New PointF(200.0F, 150.0F)
        Dim pt6 As New PointF(350.0F, 250.0F)
        Dim pt7 As New PointF(200.0F, 200.0F)

        Dim ptsArray As PointF() = {pt1, pt2, pt3, pt4, pt5, pt6, _

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

        ' Draw Bezier
        e.Graphics.DrawBeziers(redPen, ptsArray)

        ' Dispose of object

    End Sub
End Class

Figure 3.25 shows the output from Listing 3.18


FIGURE 3.25: Drawing Bezier curves


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.


More Articles

© 2020 DotNetHeaven. All rights reserved.