Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / Visual Basic
Article

Drawing Round-Edged Rectangle Using VB.NET

Rate me:
Please Sign up or sign in to vote.
3.56/5 (26 votes)
29 Apr 2004 136.4K   2.8K   17   21
This code can be used to draw Round Edged Rectangles using VB.NET

Sample Image - ImageRoundEdgeRectangle.gif

Introduction

This is a Article explains about drawing a Round-edge Rectangle using VB.Net. It will help beginners to learn 'How to make use of GDI+ in VB.NET

The input parameters are Xaxis, Yaxis, Width, Height and Diameter. Based on the diameter, the calculations are made. For an example I have put the color of the rectangle as Black.
If user want to change they can chage the color.

Hope it will be helpful.

VB.NET
Public Sub DrawRoundedRectangle(ByVal objGraphics As Graphics, _
                                ByVal m_intxAxis As Integer, _
                                ByVal m_intyAxis As Integer, _
                                ByVal m_intWidth As Integer, _
                                ByVal m_intHeight As Integer, _
                                ByVal m_diameter As Integer)

 
   
   'Dim g As Graphics
    Dim BaseRect As New RectangleF(m_intxAxis, m_intyAxis, m_intWidth, 
                                  m_intHeight)
    Dim ArcRect As New RectangleF(BaseRect.Location,
                              New SizeF(m_diameter, m_diameter))
    'top left Arc
    objGraphics.DrawArc(Pens.Black, ArcRect, 180, 90)
    objGraphics.DrawLine(Pens.Black, m_intxAxis + CInt(m_diameter / 2), 
                         m_intyAxis, 
                         m_intxAxis + m_intWidth - CInt(m_diameter / 2), 
                         m_intyAxis)

    ' top right arc
    ArcRect.X = BaseRect.Right - m_diameter
    objGraphics.DrawArc(Pens.Black, ArcRect, 270, 90)
    objGraphics.DrawLine(Pens.Black, m_intxAxis + m_intWidth, 
                         m_intyAxis + CInt(m_diameter / 2), 
                         m_intxAxis + m_intWidth, 
                         m_intyAxis + m_intHeight - CInt(m_diameter / 2))

    ' bottom right arc
    ArcRect.Y = BaseRect.Bottom - m_diameter
    objGraphics.DrawArc(Pens.Black, ArcRect, 0, 90)
    objGraphics.DrawLine(Pens.Black, m_intxAxis + CInt(m_diameter / 2), 
                         m_intyAxis + m_intHeight, 
                         m_intxAxis + m_intWidth - CInt(m_diameter / 2), 
                         m_intyAxis + m_intHeight)

    ' bottom left arc
    ArcRect.X = BaseRect.Left
    objGraphics.DrawArc(Pens.Black, ArcRect, 90, 90)
    objGraphics.DrawLine(Pens.Black, 
                         m_intxAxis, m_intyAxis + CInt(m_diameter / 2), 
                         m_intxAxis, 
                         m_intyAxis + m_intHeight - CInt(m_diameter / 2))

End Sub

You can call this method from anywhere by just giving the required values to the input parameters.You can chnage the input parameters like

VB.NET
Private Sub DrawRoundedRectangle(ByVal rectf as RectangleF, _<BR>                                 ByVal m_diameter As Integer)
and from RectangleF object You can get the values of xAxis, yAxis, width and Height of the rectangles.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Working as a S/W Engineer with 3+ years of experience
Having MCAD in .NET
Selected as Microsoft India Community Star

Comments and Discussions

 
QuestionNice. Pin
IDstrife29-Dec-19 6:38
IDstrife29-Dec-19 6:38 
GeneralMy vote of 5 Pin
Member 968587923-Apr-17 6:30
Member 968587923-Apr-17 6:30 
QuestionThis is great ... how can you do to fill the inside? Pin
Member 1208859221-Apr-17 9:00
Member 1208859221-Apr-17 9:00 
GeneralMy vote of 5 Pin
sarthakganguly21-Jul-11 21:50
sarthakganguly21-Jul-11 21:50 
General[Message Deleted] Pin
Triston J. Taylor21-Nov-08 8:30
Triston J. Taylor21-Nov-08 8:30 
GeneralI Modified this code better [modified] Pin
Triston J. Taylor21-Nov-08 9:03
Triston J. Taylor21-Nov-08 9:03 
GeneralRe: I Modified this code better Pin
Triston J. Taylor21-Nov-08 9:10
Triston J. Taylor21-Nov-08 9:10 
GeneralRe: I Modified this code better Pin
Karthikeyan Muthurajan23-Nov-08 17:36
Karthikeyan Muthurajan23-Nov-08 17:36 
GeneralRe: I Modified this code better Uh Oh! Pin
Triston J. Taylor21-Nov-08 9:59
Triston J. Taylor21-Nov-08 9:59 
GeneralThanks, Sort of Pin
Steve Clarke31-Jul-04 15:54
Steve Clarke31-Jul-04 15:54 
QuestionWhat use is this? Pin
mav.northwind30-Apr-04 3:17
mav.northwind30-Apr-04 3:17 
AnswerRe: What use is this? Pin
Uwe Keim30-Apr-04 8:00
sitebuilderUwe Keim30-Apr-04 8:00 
GeneralRe: What use is this? Pin
Karthikeyan Muthurajan2-May-04 18:57
Karthikeyan Muthurajan2-May-04 18:57 
GeneralRe: What use is this? Pin
Love.net3-May-04 19:49
Love.net3-May-04 19:49 
GeneralRe: What use is this? Pin
DDnDD3-Feb-05 11:14
DDnDD3-Feb-05 11:14 
GeneralRe: What use is this? Pin
DDnDD3-Feb-05 12:01
DDnDD3-Feb-05 12:01 
figgered it out, using the graphics path, THanks again!!!



Private Sub DrawSolidRoundedRectangle(ByVal objGraphics As Graphics, _
ByVal m_intxAxis As Integer, _
ByVal m_intyAxis As Integer, _
ByVal m_intWidth As Integer, _
ByVal m_intHeight As Integer, _
ByVal m_diameter As Integer, _
ByVal m_Brush As Brush)
Dim path As New GraphicsPath
'Dim g As Graphics
Dim BaseRect As New RectangleF(m_intxAxis, m_intyAxis, m_intWidth, m_intHeight)
Dim ArcRect As New RectangleF(BaseRect.Location, New SizeF(m_diameter, m_diameter))
'top left Arc
path.AddArc(ArcRect, 180, 90)
path.AddLine(m_intxAxis + CInt(m_diameter / 2), m_intyAxis, m_intxAxis + m_intWidth - CInt(m_diameter / 2), m_intyAxis)
' top right arc
ArcRect.X = BaseRect.Right - m_diameter
path.AddArc(ArcRect, 270, 90)
path.AddLine(m_intxAxis + m_intWidth, m_intyAxis + CInt(m_diameter / 2), m_intxAxis + m_intWidth, m_intyAxis + m_intHeight - CInt(m_diameter / 2))
' bottom right arc
ArcRect.Y = BaseRect.Bottom - m_diameter
path.AddArc(ArcRect, 0, 90)
path.AddLine(m_intxAxis + CInt(m_diameter / 2), m_intyAxis + m_intHeight, m_intxAxis + m_intWidth - CInt(m_diameter / 2), m_intyAxis + m_intHeight)
' bottom left arc
ArcRect.X = BaseRect.Left
path.AddArc(ArcRect, 90, 90)
path.AddLine(m_intxAxis, m_intyAxis + CInt(m_diameter / 2), m_intxAxis, m_intyAxis + m_intHeight - CInt(m_diameter / 2))

objGraphics.FillPath(m_Brush, path)
End Sub


=~=~=~=
PeAcE
DrDave
~=~=~=~
GeneralRe: What use is this? Pin
Graemeg3318-Jan-07 0:54
Graemeg3318-Jan-07 0:54 
GeneralRe: What use is this? Pin
phil_the16-Aug-07 23:34
phil_the16-Aug-07 23:34 
AnswerRe: What use is this? Pin
Triston J. Taylor21-Nov-08 16:23
Triston J. Taylor21-Nov-08 16:23 
GeneralRe: What use is this? Pin
JBenhart13-Nov-11 21:33
JBenhart13-Nov-11 21:33 
GeneralRe: What use is this? Pin
Anonymous24-Feb-05 13:22
Anonymous24-Feb-05 13:22 

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.