Create rectangles with rounded corners

Manipulate PDF, Shapes
1/31/2014

Downloads

A Rectangle with rounded corners is not a standard shape. However it is easy to create these

Using four freehand line segments you can create a normal rectangle. To create rectangles with rounded corners just add bezier segments, one for each corner:

C# code sample

1 // Rounded rectangle class that can be added to an overlay 2 public class RoundedRectangle : FreeHandShape 3 { 4 public RoundedRectangle(double x1, double y1, double x2, double y2, double r) 5 { 6 base.Pen = new Pen(Color.Black); // default pen 7 base.Brush = new SolidBrush(Color.White); // default brush 8 r = System.Math.Min(r, System.Math.Abs(x2 - x1) / 2); 9 r = System.Math.Min(r, System.Math.Abs(y2 - y1) / 2); 10 var path = new FreeHandPath(); 11 path.Closed = true; 12 path.Segments.Add(new FreeHandStartSegment(x1 + r, y1)); 13 path.Segments.Add(new FreeHandLineSegment(x2 - r, y1)); 14 path.Segments.Add(new FreeHandBezierSegment(x2 - r, y1, x2, y1, x2, y1 + r)); 15 path.Segments.Add(new FreeHandLineSegment(x2, y2 - r)); 16 path.Segments.Add(new FreeHandBezierSegment(x2, y2 - r, x2, y2, x2 - r, y2)); 17 path.Segments.Add(new FreeHandLineSegment(x1 + r, y2)); 18 path.Segments.Add(new FreeHandBezierSegment(x1 + r, y2, x1, y2, x1, y2 - r)); 19 path.Segments.Add(new FreeHandLineSegment(x1, y1 + r)); 20 path.Segments.Add(new FreeHandBezierSegment(x1, y1 + r, x1, y1, x1 + r, y1)); 21 this.Paths.Add(path); 22 } 23 }

VB.NET code sample

1 Public Class RoundedRectangle 2 Inherits FreeHandShape 3 Public Sub New(x1 As Double, y1 As Double, x2 As Double, y2 As Double, r As Double) 4 MyBase.Pen = New Pen(Color.Black) 5 ' default pen 6 MyBase.Brush = New SolidBrush(Color.White) 7 ' defualt brush 8 r = System.Math.Min(r, System.Math.Abs(x2 - x1) / 2) 9 r = System.Math.Min(r, System.Math.Abs(y2 - y1) / 2) 10 Dim path = New FreeHandPath() 11 path.Closed = True 12 path.Segments.Add(New FreeHandStartSegment(x1 + r, y1)) 13 path.Segments.Add(New FreeHandLineSegment(x2 - r, y1)) 14 path.Segments.Add(New FreeHandBezierSegment(x2 - r, y1, x2, y1, x2, y1 + r)) 15 path.Segments.Add(New FreeHandLineSegment(x2, y2 - r)) 16 path.Segments.Add(New FreeHandBezierSegment(x2, y2 - r, x2, y2, x2 - r, y2)) 17 path.Segments.Add(New FreeHandLineSegment(x1 + r, y2)) 18 path.Segments.Add(New FreeHandBezierSegment(x1 + r, y2, x1, y2, x1, y2 - r)) 19 path.Segments.Add(New FreeHandLineSegment(x1, y1 + r)) 20 path.Segments.Add(New FreeHandBezierSegment(x1, y1 + r, x1, y1, x1 + r, y1)) 21 Me.Paths.Add(path) 22 End Sub 23 End Class

Which draws a rectangle like this:

rectangle1.jpg

Then use this class in code like below:

1 double width = 121.35; 2 double height = 75.00; 3 double x = 30.00; 4 double y = 570.00; 5 var r1 = new RoundedRectangle(x, y, x + width, y + height, 14); 6 r1.Pen = new Pen(Color.Black, 2, new DashPattern(1, new double[] { 3, 1 })); 7 r1.Brush = new AxialGradientBrush(Color.PeachPuff, Color.Purple, x, y + height, x, y); 8 page.Overlay.Add(r1);

VB.NET code sample

1 Dim width As Double = 121.35 2 Dim height As Double = 75.0 3 Dim x As Double = 30.0 4 Dim y As Double = 570.0 5 Dim r1 = New RoundedRectangle(x, y, x + width, y + height, 14) 6 r1.Pen = New Pen(Color.Black, 2, New DashPattern(1, New Double() {3, 1})) 7 r1.Brush = New AxialGradientBrush(Color.PeachPuff, Color.Purple, x, y + height, x, y) 8 page.Overlay.Add(r1)

It is of course possible to make variations in the color, pen-size and other parameters. This code will demonstrate it:

1 // draw a couple of colored rectangles on the PDF document, 2 // demonstrating variations in color, rounding and line width 3 double x = 0; 4 double y = 0; 5 double width = 161.80 * 0.75; 6 double height = 100.00 * 0.75; 7 for (x = 30.00; x < 600.00 - width; x += width + 20) 8 { 9 for (y = 130.00; y < 500.00 - height; y += height + 20) 10 { 11 double line = (x + y) / 80; // the linewidth 12 double r = (x + y) / 20; // radius of the rounded corners 13 int grad1 = (int)x / 2; // color each rectangle different 14 int grad2 = (int)y / 2; 15 int grad3 = 100; 16 17 // create rounded rectangles, one at the time 18 var rectangle = new RoundedRectangle(x, y, x + width, y + height, r); 19 rectangle.Pen = new Pen(Color.FromArgb(grad1, grad2, grad3), line); 20 rectangle.Brush = new SolidBrush(Color.FromArgb(grad3, grad1, grad2)); 21 page.Overlay.Add(rectangle); 22 } 23 }

VB.NET code sample

1 ' demonstrating variations in color, rounding and line width 2 Dim x As Double = 0 3 Dim y As Double = 0 4 Dim width As Double = 161.8 * 0.75 5 Dim height As Double = 100.0 * 0.75 6 x = 30.0 7 While x < 600.0 - width 8 y = 130.0 9 While y < 500.0 - height 10 Dim line As Double = (x + y) / 80 11 ' the linewidth 12 Dim r As Double = (x + y) / 20 13 ' radius of the rounded corners 14 Dim grad1 As Integer = CInt(x) / 2 15 ' color each rectangle different 16 Dim grad2 As Integer = CInt(y) / 2 17 Dim grad3 As Integer = 100 18 19 ' create rounded rectangles, one at the time 20 Dim rectangle = New RoundedRectangle(x, y, x + width, y + height, r) 21 rectangle.Pen = New Pen(Color.FromArgb(grad1, grad2, grad3), line) 22 rectangle.Brush = New SolidBrush(Color.FromArgb(grad3, grad1, grad2)) 23 page.Overlay.Add(rectangle) 24 y += height + 20 25 End While 26 x += width + 20 27 End While

Which will give the following result:

rectangles.jpg