ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Fill/Draw RoundedRectangle

C# extension to Fill and or Draw RoundedRectangle

Source

public static void FillRoundedRectangle( this Graphics g, Pen pen, Brush brush, int x, int y, int width, int height, int radius )
		{
			Rectangle corner = new Rectangle( x, y, radius, radius );
			GraphicsPath path = new GraphicsPath( );
			path.AddArc( corner, 180, 90 );
			corner.X = x + width - radius;
			path.AddArc( corner, 270, 90 );
			corner.Y = y + height - radius;
			path.AddArc( corner, 0, 90 );
			corner.X = x;
			path.AddArc( corner, 90, 90 );
			path.CloseFigure( );

			g.FillPath( brush, path );

			if( pen != null )
			{
				g.DrawPath( pen, path );
			}
		}

Example

protected override void OnPaint( PaintEventArgs e )
		{
			e.Graphics.FillRoundedRectangle( new Pen( Color.Black ), null, 0, 0, 200, 200, 12 );
		}

Author: Jerry Knauss

Submitted on: 22 mrt. 2014

Language: C#

Type: System.Draw

Views: 8730