SIGN UP MEMBER LOGIN:    
ARTICLE

Drawing Bezier Curves in GDI+

Posted by Mahesh Chand Articles | GDI+ & Graphics November 26, 2009
In this article I will explain you how to draw a Bezier Curve in GDI+.
Reader Level:
Download Files:
 

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.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor