How to solve ArgumentException : The parameter is not valid for drawing Arcs

I'm making a custom winforms button in VB.Net with rounded edges and other features. I create a path using various inputs defined by the user and draw and fill it using pens and brushes.

When I call e.Graphics.FillEllipse(Brush1, Rect1) and e.Graphics.DrawEllips(Pen1, Rect1) it just works fine without any problems, but when I try e.Graphics.FillPath(Brush1, OuterPath) and e.Graphics.DrawPath(Pen1, OuterPath) it doesn't work at all. I get this error:

ArgumentException: The parameter is not valid

I tried giving the right types of each variable used in the process and not letting the compiler decide, creating more variables to calculate and manage the inputs individually to not make all the calculations in the inputs of each function, which makes my work easier honestly, and even using the CType function in the inputs of each function to make sure that the function understands what I want as inputs. But everything failed and I don't know what to do next to fix the issue.

Here is the code:

Private Sub MetaniumButton_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    Dim PathWidth As Integer = Width - BorderSize / 2
    Dim PathHeight As Integer = Height - BorderSize / 2

    _Roundnes = RoundnesMemory

    If PathHeight < Roundenes.Height Then
        _Roundnes.Height = PathHeight - 1
    End If

    If PathWidth < Roundenes.Width Then
        _Roundnes.Width = PathWidth - 1
    End If

    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

    Dim OuterPath As New GraphicsPath

    Dim Rec1 As Rectangle = New Rectangle(CType(BorderSize / 2, Int32), CType(BorderSize / 2, Int32), CType(_Roundnes.Width, Int32), CType(_Roundnes.Height, Int32))
    Dim Rec2 As Rectangle = New Rectangle(PathWidth - _Roundnes.Width, BorderSize / 2, _Roundnes.Width, _Roundnes.Height)
    Dim Rec3 As Rectangle = New Rectangle(PathWidth - _Roundnes.Width, PathHeight - _Roundnes.Height, _Roundnes.Width, _Roundnes.Height)
    Dim Rec4 As Rectangle = New Rectangle(BorderSize / 2, PathHeight - _Roundnes.Height, _Roundnes.Width, _Roundnes.Height)

    OuterPath.StartFigure()

    OuterPath.AddLine(CInt(_Roundnes.Width / 2 + BorderSize / 2), CInt(BorderSize / 2), CInt(PathWidth - _Roundnes.Width / 2), CInt(BorderSize / 2))
    OuterPath.AddArc(Rec1, 180.0, 90.0) ' Here is the problem and it could probably in any AddArc Function i used

    OuterPath.AddLine(PathWidth, CInt(_Roundnes.Height / 2 + BorderSize / 2), PathWidth, CInt(PathHeight - _Roundnes.Height / 2))
    OuterPath.AddArc(Rec2, -90, 90)

    OuterPath.AddLine(CInt(_Roundnes.Width / 2 + BorderSize / 2), PathHeight, CInt(PathWidth - _Roundnes.Width / 2), PathHeight)
    OuterPath.AddArc(Rec3, 0, 90)

    OuterPath.AddLine(CInt(BorderSize / 2), CInt(_Roundnes.Height / 2), CInt(BorderSize / 2), CInt(PathHeight - _Roundnes.Height / 2))
    OuterPath.AddArc(Rec4, 90, 90)

    OuterPath.CloseFigure()

    e.Graphics.FillPath(Brush1, OuterPath)
    e.Graphics.DrawPath(Pen1, OuterPath)

    Dim LabelCount As Integer = 0
    For Each l As Label In Controls
        LabelCount += 1
    Next
    Dim TextPlace As New Label With {.Name = "TextLabel",
                                     .Text = Text,
                                     .AutoEllipsis = True,
                                     .Size = New Size(Width -
 Margin.Left + Margin.Right + 2 * _Roundnes.Width) / 2, Height - (Margin.Top + Margin.Bottom + 2 * _Roundnes.Height) / 2),
                                     .TextAlign = _TextAlign,
                                     .ForeColor = _FontColor,
                                     .BackColor = _MetaniumBackColor,
                                     .Location = New Point((Width - .Width) / 2, (Height - .Height) / 2)}

    AddHandler TextPlace.TextChanged, AddressOf MetaniumButton_TextChanged
    AddHandler Me.TextChanged, AddressOf MetaniumButton_TextChanged
    Controls.Add(TextPlace)
    T += 1
    If LabelCount <= 0 Then
0:      For Each l As Label In Controls
            If l.Name = "TextLabel" Then
                l.Text = Text
                l.AutoEllipsis = True
                l.Size = New Size(Width - (Margin.Left + Margin.Right + 2 * _Roundnes.Width) / 2, Height - (Margin.Top + Margin.Bottom + 2 * _Roundnes.Height) / 2)
                l.TextAlign = _TextAlign
                l.ForeColor = _FontColor
                l.BackColor = _MetaniumBackColor
                l.Location = New Point((Width - l.Width) / 2, (Height - l.Height) / 2)
            End If
        Next
    ElseIf LabelCount = 1 Then
        For Each l As Label In Controls
            If l.Name <> "TextLabel" Then
                Controls.Remove(l)
            Else
                GoTo 1
            End If
1:          GoTo 0
        Next
    Else
    End If
End Sub

When I track down the bug it seems the problem is in the AddArc() function, and I really don't know why it doesn't work. Any help appreciated.

BTW, I use VB.Net Express 2010 with .Net Framework 4.8.

PS: you can post an answer using either VB.Net or C# I can translate the code from both of them.



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation