Results 1 to 10 of 10

Thread: Rounded rectangles?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Rounded rectangles?

    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
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Junior Member
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    22
    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

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    well I made a crappy function. Doesnt work perfectly, and it may not make a lot of sense

    VB Code:
    1. Private Function GetRoundedRect(ByVal rect As Rectangle, ByVal roundness As Integer) As GraphicsPath
    2.         If roundness < 1 Then
    3.             Throw New ArgumentOutOfRangeException("Roundness has to be greater than or equal to one.")
    4.         End If
    5.  
    6.         Dim MAX_ROUNDNESS As Integer = NumberManipulation.LeastValue(rect.Width \ 2, rect.Height \ 2)
    7.         If roundness > MAX_ROUNDNESS Then roundness = MAX_ROUNDNESS
    8.  
    9.  
    10.         Dim p As New GraphicsPath()
    11.         Dim roundRect As New Rectangle(rect.X, rect.Y, 2 * roundness, 2 * roundness)
    12.  
    13.         p.AddArc(roundRect, 180, 90)
    14.  
    15.         roundRect.X = rect.Right - roundRect.Width
    16.         p.AddArc(roundRect, -90, 90)
    17.  
    18.         roundRect.Y = rect.Bottom - roundRect.Height
    19.         p.AddArc(roundRect, 0, 90)
    20.  
    21.         roundRect.X = rect.X
    22.         p.AddArc(roundRect, 90, 90)
    23.  
    24.         p.CloseFigure()
    25.         Return p
    26.     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
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Junior Member
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    22
    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
    Attached Images Attached Images  

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim mygraphics As Graphics
    3.         mygraphics = Graphics.FromHwnd(ActiveForm.Handle)
    4.  
    5.         DrawRoundRectangle(140, 120, 29, 30, 15, Color.Blue, Color.White, mygraphics)
    6.     End Sub
    7.     Function DrawRoundRectangle(ByVal X1 As Integer, ByVal Y1 As Integer, ByVal Length As Single, ByVal Height As Single, _
    8.   ByVal RoundRadius As Integer, ByVal LineColor As Color, ByVal FillColor As Color, ByVal gr As System.Drawing.Graphics) As System.Drawing.Graphics
    9.  
    10.  
    11.         Dim Pen As New System.Drawing.Pen(LineColor)
    12.         Dim Brush As New System.Drawing.SolidBrush(FillColor)
    13.  
    14.         Dim myPath As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath()
    15.         [b]If RoundRadius > Length / 2 OrElse RoundRadius > Height / 2 Then RoundRadius=Height / 2[/b]
    16.  
    17.         myPath.StartFigure()
    18.  
    19.         myPath.AddArc(X1, Y1, RoundRadius * 2, RoundRadius * 2, 180, 90)
    20.         myPath.AddArc(X1 + Length - RoundRadius * 2, Y1, RoundRadius * 2, RoundRadius * 2, 270, 90)
    21.         myPath.AddArc(X1 + Length - RoundRadius * 2, Y1 + Height - RoundRadius * 2, RoundRadius * 2, RoundRadius * 2, 0, 90)
    22.         myPath.AddArc(X1, Y1 + Height - RoundRadius * 2, RoundRadius * 2, RoundRadius * 2, 90, 90)
    23.         myPath.CloseFigure()
    24.  
    25.         gr.FillPath(Brush, myPath)
    26.         gr.DrawPath(Pen, myPath)
    27.  
    28.         Return gr
    29.  
    30.     End Function
    Last edited by nemaroller; Apr 10th, 2003 at 08:46 AM.

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Btw, this has proved very helpful in making my RoundButton class.... thanks again.... I now have beautiful rounded-edge gradient- filled buttons...
    Last edited by nemaroller; Apr 10th, 2003 at 02:40 PM.

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.


    Attached Images Attached Images  
    Last edited by nemaroller; Apr 10th, 2003 at 03:24 PM.

  9. #9

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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...
    w00t, looks nice
    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).
    mine had that

    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
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  10. #10
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I haven't noticed a slightly less round corner, but I have been using
    VB Code:
    1. 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...
    Last edited by nemaroller; Apr 10th, 2003 at 03:36 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width