1

I would like to draw a rectangle with rounded edges.

The "roundness" should stay consistent, no matter which size (except a minimum size of let's say 50x50 pixels) the rectangle has.

My current approach looks like the one below. It doesn't take the size of the rectangle into account. I use 45 as the arcsize.

How could I make the roundness consistent over different rectangle sizes?

Thank you.

Public Function RoundedRect(ByVal uRect As Rectangle, ByVal uArchSize As Integer) As GraphicsPath

    Dim nPath As New GraphicsPath

    If uArchSize > 0 Then
        nPath.AddArc(Rectangle.FromLTRB(uRect.Right - uArchSize, uRect.Top, uRect.Right, uRect.Top + uArchSize), -90, 90)
        nPath.AddArc(Rectangle.FromLTRB(uRect.Right - uArchSize, uRect.Bottom - uArchSize, uRect.Right, uRect.Bottom), 0, 90)
        nPath.AddArc(Rectangle.FromLTRB(uRect.Left, uRect.Bottom - uArchSize, uRect.Left + uArchSize, uRect.Bottom), 90, 90)
        nPath.AddArc(Rectangle.FromLTRB(uRect.Left, uRect.Top, uRect.Left + uArchSize, uRect.Top + uArchSize), 180, 90)
        nPath.CloseFigure()
    Else
        nPath.AddRectangle(uRect)
    End If

    Return nPath

End Function
tmighty
  • 10,734
  • 21
  • 104
  • 218
  • 1
    You need to define what _consistent_ means to you. One way would be to set the arc radius to be percentage of the rectangle's minimum dimension (width or height). – TnTinMn Mar 23 '18 at 00:45
  • 1
    You could use an ArchSizeX (Left, Right) and ArchSizeY(Top, Bottom), transforming `ArchSize` based on the relation between the Rectangle Width and Height variations. Something like: `[ArchSizeX] = ArchSize + ((Rect.Width - Rect.Height) / ArchSize)` <=> `[ArchSizeY] = ArchSize + ((Rect.Height - Rect.Width) / ArchSize)`. They should scale proportionally. – Jimi Mar 23 '18 at 01:58
  • [How to create a User Control with rounded corners?](https://stackoverflow.com/a/32991419/3110834) – Reza Aghaei Mar 23 '18 at 03:04

0 Answers0