Draw Rounded Corner Rectangles Using C#

Draw Rounded Corner Rectangles Using C#
Use the GDI library objects to draw rectangles that have rounded rectangles of a specified radius. The secret is to use a series of lines and arcs to form the rectangle as desired.
1. public void DrawRoundRect(Graphics g, Pen p, float x, float y, float 
   width, float height, float radius)
2.   {
3.       GraphicsPath gp = new GraphicsPath();
4. 
 5.      gp.AddLine(x + radius, y, x + width - (radius * 2), y); // Line
6.       gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 
    90); // Corner
7.       gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 
    2)); // Line
8.       gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius 
    * 2, radius * 2, 0, 90); // Corner
9.       gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + 
    height); // Line
 10.     gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 
    90); // Corner
11.      gp.AddLine(x, y + height - (radius * 2), x, y + radius); // Line
12.      gp.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Corner
13.      gp.CloseFigure();
14. 
 15.     g.DrawPath(p, gp);
16.      gp.Dispose();
17.}

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top