.NET GDI+: Drawing lines with rounded corners

后端 未结 3 952
名媛妹妹
名媛妹妹 2021-01-21 09:52

Given an array of points, it is easy to draw a line based on these, e.g. using the GraphicsPath class.

For instance, the following array of points...

[0]         


        
相关标签:
3条回答
  • 2021-01-21 10:11

    This is the function I use to draw a rectangle with rounded corners... from this you may calculate the angle of each line.

    Public Sub DrawRoundRect(ByVal g As Graphics, ByVal p As Pen, ByVal x As Single, ByVal y As Single, ByVal width As Single, ByVal height As Single, ByVal radius As Single)
        Dim gp As GraphicsPath = New GraphicsPath
        gp.AddLine(x + radius, y, x + width - (radius * 2), y)
        gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90)
        gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2))
        gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90)
        gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height)
        gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90)
        gp.AddLine(x, y + height - (radius * 2), x, y + radius)
        gp.AddArc(x, y, radius * 2, radius * 2, 180, 90)
        gp.CloseFigure()
        g.DrawPath(p, gp)
        gp.Dispose()
    End Sub
    

    Hope this help you in the harder part of trigonometry ;)

    0 讨论(0)
  • 2021-01-21 10:24

    Bezier curves are pretty straightforward to implement:

    http://www.codeproject.com/KB/recipes/BezirCurves.aspx

    Luckily you also have them as part of the GraphicsPath class if you wanna omit the gory details:

    http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath.addbezier.aspx

    And you can also look into splines:

    http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath.addcurve.aspx

    0 讨论(0)
  • 2021-01-21 10:29

    This url has a description of how to draw rounded rectangles that might help you start out.

    But I would think that if nothing else you would be able to add more points to your path, to give the illusion of rounded corners. So add in several points between 0,0 and 100,0. An example might be:

    (0,0) (90,0) (95,5) (95,10) (0,100)

    I have not tested that path in any way, just pulled some numbers that might work out of the air :).

    0 讨论(0)
提交回复
热议问题