• entries
    707
  • comments
    1173
  • views
    434122

C# Rounded Rectangles

Sign in to follow this  

325 views

First off, thanks for all of the replies to my last post guys! And thanks to Drew for the WR:TfJL link!

For anyone that is looking for code to draw rounded rectangles in C# using System.Drawing (and System.Drawing.Drawing2D), here's the code that I came up with:

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

namespace Beals.Graphics.Drawing
{
class GraphicsEx
{
static GraphicsPath MakeRoundedRectanglePath(RectangleF Rectangle, float Radius)
{
GraphicsPath Path = new GraphicsPath();
if(Radius <= 1.0f)
{
Path.AddRectangle(Rectangle);
Path.CloseFigure();
return Path;
}

float Diameter = Radius * 2.0f;
Path.AddArc(new RectangleF(Rectangle.Right - Diameter, Rectangle.Bottom - Diameter, Diameter, Diameter), 0, 90);
Path.AddArc(new RectangleF(Rectangle.Left, Rectangle.Bottom - Diameter, Diameter, Diameter), 90, 90);
Path.AddArc(new RectangleF(Rectangle.Left, Rectangle.Top, Diameter, Diameter), 180, 90);
Path.AddArc(new RectangleF(Rectangle.Right - Diameter, Rectangle.Top, Diameter, Diameter), 270, 90);
Path.CloseFigure();
return Path;
}

static GraphicsPath MakeRoundedRectanglePath(RectangleF Rectangle, float Radius0, float Radius1, float Radius2, float Radius3)
{
GraphicsPath Path = new GraphicsPath();
if(Radius0 * Radius1 * Radius2 * Radius3 <= 1.0f)
{
Path.AddRectangle(Rectangle);
Path.CloseFigure();
return Path;
}

Path.AddArc(new RectangleF(Rectangle.Right - Radius2 * 2, Rectangle.Bottom - Radius2 * 2, Radius2 * 2, Radius2 * 2), 0, 90);
Path.AddArc(new RectangleF(Rectangle.Left, Rectangle.Bottom - Radius3 * 2, Radius3 * 2, Radius3 * 2), 90, 90);
Path.AddArc(new RectangleF(Rectangle.Left, Rectangle.Top, Radius0 * 2, Radius0 * 2), 180, 90);
Path.AddArc(new RectangleF(Rectangle.Right - Radius1 * 2, Rectangle.Top, Radius1 * 2, Radius1 * 2), 270, 90);
Path.CloseFigure();
return Path;
}

public static void DrawRoundedRectangle(System.Drawing.Graphics Gfx, Pen Pen, int X, int Y, int Width, int Height, float Radius)
{
Gfx.DrawPath(Pen, MakeRoundedRectanglePath(new RectangleF((float)X, (float)Y, (float)Width - 1, (float)Height - 1), Radius));
}

public static void DrawRoundedRectangle(System.Drawing.Graphics Gfx, Pen Pen, int X, int Y, int Width, int Height, float Radius0, float Radius1, float Radius2, float Radius3)
{
Gfx.DrawPath(Pen, MakeRoundedRectanglePath(new RectangleF((float)X, (float)Y, (float)Width - 1, (float)Height - 1), Radius0, Radius1, Radius2, Radius3));
}

public static void FillRoundedRectangle(System.Drawing.Graphics Gfx, Brush Brush, int X, int Y, int Width, int Height, float Radius0, float Radius1, float Radius2, float Radius3)
{
Gfx.FillPath(Brush, MakeRoundedRectanglePath(new RectangleF((float)X, (float)Y, (float)Width - 1, (float)Height - 1), Radius0, Radius1, Radius2, Radius3));
}

public static void FillRoundedRectangle(System.Drawing.Graphics Gfx, Brush Brush, int X, int Y, int Width, int Height, float Radius)
{
Gfx.FillPath(Brush, MakeRoundedRectanglePath(new RectangleF((float)X, (float)Y, (float)Width - 1, (float)Height - 1), Radius));
}
}
}




And the code that drew the above:

GraphicsEx.FillRoundedRectangle(e.Graphics, new SolidBrush(this.CaptionColor), 0, 0, Size.Width, CaptionHeight, 15.0f, 15.0f, 1.0f, 1.0f);
GraphicsEx.DrawRoundedRectangle(e.Graphics, new Pen(BorderColor), 0, 0, Size.Width, CaptionHeight, 15.0f, 15.0f, 1.0f, 1.0f);
GraphicsEx.FillRoundedRectangle(e.Graphics, new SolidBrush(this.BodyColor), 0, CaptionHeight, Size.Width, Size.Height - CaptionHeight, 1.0f, 1.0f, 8.0f, 8.0f);
GraphicsEx.DrawRoundedRectangle(e.Graphics, new Pen(BorderColor), 0, 0, Size.Width, Size.Height, 15.0f, 15.0f, 8.0f, 8.0f);




This probably isn't the 'best' code, but mind you, I haven't had much sleep, I'm sick, and I wasn't worried about speed, just results [grin]. If somebody feels like adding code that accounts for 0 radii (so that you get actual corners rather than arcs with a radius of 1) and adding code that accounts for code that pretty much renders an ellipsis, go for it. If you're feeling generous, let me know about it and I might update the code here (with your permission.) I wasn't really worried because I actually like the arc-1 corners and if you're going to draw rounded rectangles that look like ellipsis...then just draw ellipsis lol.

Anyway, hopefully that helps somebody out there.

[edit]
Btw, if anybody could give me an idea of how to fill with a gradient, I'd greatly appreciate it, but for some reason, I get the feeling that it's not going to be easy (and I mean for something like the above; I can fill a rectangle with a gradient.)

Thanks!
Sign in to follow this  


3 Comments


Recommended Comments

Might be worth checking this out:

http://www.folklore.org/StoryView.py?story=Round_Rects_Are_Everywhere.txt

The rest of the site is pretty interesting too :)

Share this comment


Link to comment
Quote:
Original post by nerd_boy
You owe me a chocochip cookie, Programmer16.


One chocochip cookie for you, nerd_boy:

ENJOY!

(Btw, thanks!!! :D)

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now