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;
    }
}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source