c# draw rectangle with rounded corners code example

Example: rounded rectangle in C#

using System.Drawing;
using System.Drawing.Drawing2D;

// this code gives you the ability to create nice-looking RoundedRectangles with little-to-no AntiAliasing
// which makes it almost look like an HTML element... yes...

internal static class Converter
{
	public static Pen ToPen(this Brush b)
    {
    	return new Pen(b);
    }
}

internal sealed class RoundRect1
{
	public static GraphicsPath CreateRoundRect(int x, int y, int w, int h, int r)
	{
		int r2 = r * 2;

		GraphicsPath p = new GraphicsPath();
		p.AddArc(x, y, r2, r2, 180, 90);
		//p.AddLine(x + r, y, x + w - r, y);
		p.AddArc(x + w - r2, y, r2, r2, 270, 90);
		//p.AddLine(x + w, y + r, x + w, y + h - r);
		p.AddArc(x + w - r2, y + h - r2, r2, r2, 0, 90);
		//p.AddLine(x + r, y + h, x + w - r, y + h);
		p.AddArc(x, y + h - r2, r2, r2, 90, 90);
		p.CloseFigure();
		return p;
	}

	public void FillArea(Rectangle area, int radius, Color fillColor)
    {
      GraphicsPath gfxPath = RoundRect1.CreateRoundRect(area.X, area.Y, area.Width, area.Height, radius);
      Region region = new Region(gfxPath);

      SmoothingMode originalAliasing = gfx.SmoothingMode;
      gfx.SmoothingMode = SmoothingMode.HighQuality;
      gfx.FillPath(fillColor.ToBrush(), gfxPath);
      gfx.DrawPath(new Pen(fillColor), gfxPath);
      gfx.SetClip(region, CombineMode.Replace);
      gfx.SmoothingMode = originalAliasing;
    }
}