is there an easy way to draw a rounded rectangle? right now I'm making a graphicsPath and I'm making one by drawing 4 arcs and 4 lines and connecting them together. It's kinda messy:rolleyes:
Printable View
is there an easy way to draw a rounded rectangle? right now I'm making a graphicsPath and I'm making one by drawing 4 arcs and 4 lines and connecting them together. It's kinda messy:rolleyes:
I like to take this thread up again... since I need to draw rounded rectangles as well...
you guys might have a few more months of exprience of vb.net now :)
if there is no good solution I would really appreciate some code-snippets of the arc-solution written about above.
Thanks in advance
David N.
Sweden
well I made a crappy function. Doesnt work perfectly, and it may not make a lot of sense:D
VB Code:
Private Function GetRoundedRect(ByVal rect As Rectangle, ByVal roundness As Integer) As GraphicsPath If roundness < 1 Then Throw New ArgumentOutOfRangeException("Roundness has to be greater than or equal to one.") End If Dim MAX_ROUNDNESS As Integer = NumberManipulation.LeastValue(rect.Width \ 2, rect.Height \ 2) If roundness > MAX_ROUNDNESS Then roundness = MAX_ROUNDNESS Dim p As New GraphicsPath() Dim roundRect As New Rectangle(rect.X, rect.Y, 2 * roundness, 2 * roundness) p.AddArc(roundRect, 180, 90) roundRect.X = rect.Right - roundRect.Width p.AddArc(roundRect, -90, 90) roundRect.Y = rect.Bottom - roundRect.Height p.AddArc(roundRect, 0, 90) roundRect.X = rect.X p.AddArc(roundRect, 90, 90) p.CloseFigure() Return p End Function
just pass the rectangle that you want to draw, and give it a value for "roundness".... it returns a path. I didnt write any comments, sorry :D
all-right... I modified a little and it works almost great
when I use the FillPath() method (system.drawing.graphics), it fills the path but it also draws some wierd lines...
watch the pic
Code:Function DrawRoundRectangle(ByVal X1 As Integer, ByVal Y1 As Integer, ByVal Length As Single, ByVal Height As Single, ByVal RoundRadius As Integer, ByVal LineCol As String, ByVal FillCol As String, ByVal gr As System.Drawing.Graphics) As System.Drawing.Graphics
Dim LineColor As System.Drawing.Color = GetColor(LineCol)
Dim FillColor As System.Drawing.Color = GetColor(FillCol)
Dim Pen As New System.Drawing.Pen(LineColor)
Dim Brush As New System.Drawing.SolidBrush(FillColor)
Dim myPath As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath()
myPath.StartFigure()
myPath.AddArc(X1, Y1, RoundRadius * 2, RoundRadius * 2, 180, 90)
myPath.AddArc(X1 + Length - RoundRadius * 2, Y1, RoundRadius * 2, RoundRadius * 2, 270, 90)
myPath.AddArc(X1 + Length - RoundRadius * 2, Y1 + Height - RoundRadius * 2, RoundRadius * 2, RoundRadius * 2, 0, 90)
myPath.AddArc(X1, Y1 + Height - RoundRadius * 2, RoundRadius * 2, RoundRadius * 2, 90, 90)
myPath.CloseFigure()
gr.FillPath(Brush, myPath)
gr.DrawPath(Pen, myPath)
Return gr
End Function
to tell you the truth I'm tired of the graphics class of .NET, it can be due to my own dumb-ass-ness, but anyways I'm tired of it:( I get billions of weird errors
I dont remember having that kind of error when I used the function, sorry if I cant help
Zcumbag's functions works rather well....
the only thing missing is to check whether the value for Roundness is greater than the (Length or Width /2)... because once roundness is > than those, it is beyond the abilities of the function (since you are basically asking for an ellipse at that point).
So pat yourself on the back, because its perfect.... the 'errors' you asked about were actually side effects of supplying bad parameters to the function. AS long as Roundness is not greater than HALF the length or width, it works wonderfully.
Btw, I changed the passed colors to be Colors instead of strings...
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim mygraphics As Graphics mygraphics = Graphics.FromHwnd(ActiveForm.Handle) DrawRoundRectangle(140, 120, 29, 30, 15, Color.Blue, Color.White, mygraphics) End Sub Function DrawRoundRectangle(ByVal X1 As Integer, ByVal Y1 As Integer, ByVal Length As Single, ByVal Height As Single, _ ByVal RoundRadius As Integer, ByVal LineColor As Color, ByVal FillColor As Color, ByVal gr As System.Drawing.Graphics) As System.Drawing.Graphics Dim Pen As New System.Drawing.Pen(LineColor) Dim Brush As New System.Drawing.SolidBrush(FillColor) Dim myPath As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath() [b]If RoundRadius > Length / 2 OrElse RoundRadius > Height / 2 Then RoundRadius=Height / 2[/b] myPath.StartFigure() myPath.AddArc(X1, Y1, RoundRadius * 2, RoundRadius * 2, 180, 90) myPath.AddArc(X1 + Length - RoundRadius * 2, Y1, RoundRadius * 2, RoundRadius * 2, 270, 90) myPath.AddArc(X1 + Length - RoundRadius * 2, Y1 + Height - RoundRadius * 2, RoundRadius * 2, RoundRadius * 2, 0, 90) myPath.AddArc(X1, Y1 + Height - RoundRadius * 2, RoundRadius * 2, RoundRadius * 2, 90, 90) myPath.CloseFigure() gr.FillPath(Brush, myPath) gr.DrawPath(Pen, myPath) Return gr End Function
Btw, this has proved very helpful in making my RoundButton class.... thanks again.... I now have beautiful rounded-edge gradient- filled buttons...
Here, I attached a picture of the buttons... which use the roundrectangle function provided above... notice the bottom picture uses a circular gradient instead of a forwarddiagonal gradient.
http://www.sofa.com/roundbuttons.jpg
http://www.sofa.com/roundbuttons2.jpg
w00t, looks nice :)Quote:
Originally posted by nemaroller
Btw, this has proved very helpful in making my RoundButton class.... thanks again.... I now have beautiful rounded-edge gradient- filled buttons...
mine had that:DQuote:
the only thing missing is to check whether the value for Roundness is greater than the (Length or Width /2)... because once roundness is > than those, it is beyond the abilities of the function (since you are basically asking for an ellipse at that point).
edit: btw if you notice I said in the first post that it is kinda "crappy". Well it doesnt make a perfect rectangle. Usually it's not obvious, but with certain values of roundness it gets more obvious... the bottom right corner is usually less round than the other corners
I haven't noticed a slightly less round corner, but I have been using
VB Code:
graphics.SmoothingMode = SmoothingMode.AntiAlias
in my button class, so it always seems to turn out perfect... but it may have to do with the multiplication and a rounding error.... i'll look into that...