Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / C#
Article

Rounded Corners

Rate me:
Please Sign up or sign in to vote.
4.25/5 (28 votes)
29 May 2006CPOL2 min read 101.6K   6.8K   44   10
Easy draw rounded corners
Sample Image - result.gif

Introduction

I will not be opening the big secret if I say that the library of GDI+ has gained a lot of popularity from developers of Win32 applications. Microsoft uses it in Office applications, for example Visio (I shall risk assuming, that this library has been given birth by this development). That fact became natural as .NET Framework GDI+ has come without any essential changes. As a matter of fact, classes for work with graphics are only the shell for methods of GDI+. Therefore my article will be useful for developers using GDI+ and for those who use the .NET Framework in their work.

Using the Code

While creating a small vector editor, I faced a small problem connected with rounding of corners for rectangular figures. Unfortunately, Microsoft has not provided such an ability in GDI+. Certainly it was possible to go the simple way and use a class stated in the article, Extended Graphics - An implementation of Rounded Rectangle in C#, but unfortunately this allows creating only rounded rectangles. I needed to round off corners between any direct lines as it does in Visio with such ease. Thus I had to recollect a school course of geometry and create a class, allowing rounding off corners between any set of lines consistently incorporating among themselves.

To use this class, this is simple enough. While creating a copy of the class Cpp.Diagram.Rounded.Corners, it is necessary to specify a set of points through which we pass lines, and also the radius of a rounding.

C#
GraphicsPath path = new GraphicsPath ();
....
Cpp.Diagram.Rounded.Corners r = new Cpp.Diagram.Rounded.Corners(points, 5);
r.Execute(path);

In a variable ”path”, the graphic path which can be drawn subsequently on any graphic surface will contain it. In the figure, the result of the work of the test program shows the principle of work with my class.

History

  • 29th May, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNot easy to use Pin
Xmen Real 12-Jun-10 4:32
professional Xmen Real 12-Jun-10 4:32 
GeneralRectangle with Rounded Corners and Open top Pin
phanf24-Nov-08 13:14
phanf24-Nov-08 13:14 
Questionneed help using your magnificent code Pin
Shimmy Weitzhandler18-Feb-08 0:41
Shimmy Weitzhandler18-Feb-08 0:41 
Generalone rounded corner Pin
133mmxtr17-May-07 22:31
133mmxtr17-May-07 22:31 
GeneralRe: one rounded corner Pin
Pavel Tsukanov18-May-07 2:29
Pavel Tsukanov18-May-07 2:29 
GeneralFantastic! Pin
Jason Troitsky15-Mar-07 9:55
Jason Troitsky15-Mar-07 9:55 
GeneralThank you! Pin
SMan00717-Dec-06 6:44
SMan00717-Dec-06 6:44 
GeneralExcellent - Thank you! Pin
Mr. Accident3-Sep-06 8:24
Mr. Accident3-Sep-06 8:24 
GeneralThanks, tell us more.. Pin
Yiyi Sun29-May-06 5:50
Yiyi Sun29-May-06 5:50 
GeneralRe: Thanks, tell us more.. [modified] Pin
Pavel Tsukanov29-May-06 6:37
Pavel Tsukanov29-May-06 6:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.