Results 1 to 4 of 4

Thread: Smoothing the Pen

  1. #1
    Junior Member
    Join Date
    Aug 12
    Posts
    20

    Smoothing the Pen

    I can't make the Pen smooth. If I adjusted the Size of the pen, it'll produce crack/gap. I don't have any idea to fix it. Check the image.

    Here's the code.
    Code:
    Public Class PaintFormDim PenWidth As Single = 1.0F
        Dim PenPoint As Pen
    Code:
    Sub ReloadPen(ByVal PenWd As Single, ByVal CurColor As Color)       PenPoint = New Pen(CurColor, PenWd)
    
    
       End Sub
    Code:
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Left Then
                If drawing = False Then
    
    
                    startLocation = e.Location
    
    
                    drawing = True
    
    
                    If MultiAngleRadioButton.Checked Then
    
    
                        If TempLocation.X = -1 Then
                            TempLocation = startLocation
                            TempLocation2 = startLocation
                        End If
    
    
                        endLocation = e.Location
                        g.DrawLine(PenPoint, TempLocation, endLocation)
                        TempLocation = endLocation
    
    
                    ElseIf TriangleRadioButton.Checked Then
                        If TempLocation.X = -1 Then
                            TempLocation = startLocation
                            TempLocation2 = startLocation
                        End If
    
    
                        If NumberOfAngle <= 2 Then
    
    
                            endLocation = e.Location
                            g.DrawLine(PenPoint, TempLocation, endLocation)
                            TempLocation = endLocation
    
    
                            If NumberOfAngle = 2 Then
                                g.DrawLine(PenPoint, TempLocation, TempLocation2)
                                TempLocation = New Point(-1, -1)
                                NumberOfAngle = 0
                            Else
                                NumberOfAngle += 1
                            End If
    
    
    
    
                        End If
    
    
    
    
                    End If
    
    
    
    
                End If
    
    
            ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
    
    
                If MultiAngleRadioButton.Checked Then
                    If TempLocation.X <> -1 Then
                        endLocation = e.Location
                        g.DrawLine(PenPoint, TempLocation, TempLocation2)
                        TempLocation = New Point(-1, -1)
                    End If
    
    
                End If
    
    
            End If
    
    
        End Sub
    Code:
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If drawing = True Then
                If LineRadioButton.Checked Then
                    g.DrawLine(PenPoint, startLocation.X, startLocation.Y, e.X, e.Y)
                    startLocation = e.Location
                    UpdateImage()
    
    
                ElseIf EraserRadioButton.Checked Then
    
    
                    Dim p As New Pen(Color.White, PenWidth)
    
    
                    g.DrawLine(p, startLocation.X, startLocation.Y, e.X, e.Y)
                    startLocation = e.Location
                    UpdateImage()
    
    
                End If
    
    
            End If
        End Sub
    Code:
    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    
    
            If drawing Then
    
    
                If RectangleRadioButton.Checked Then
    
    
                    endLocation = e.Location
    
    
                    Dim s As Point
    
    
                    s.X = endLocation.X - startLocation.X
                    If s.X < 0 Then
                        startLocation.X = endLocation.X
    
    
                    End If
    
    
                    s.Y = endLocation.Y - startLocation.Y
                    If s.Y < 0 Then
                        startLocation.Y = endLocation.Y
    
    
                    End If
    
    
                    s.X = Math.Abs(s.X)
                    s.Y = Math.Abs(s.Y)
                    g.DrawRectangle(PenPoint, New Rectangle(startLocation, s))
    
    
                ElseIf GradientRectAngleRadioButton.Checked Then
    
    
                    endLocation = e.Location
    
    
                    Dim s As Point
    
    
    
    
                    If s.X < 0 Then
                        startLocation.X = endLocation.X
                    ElseIf s.X = 0 Then
                        s.X = 1
                    End If
    
    
                    s.Y = endLocation.Y - startLocation.Y
    
    
                    If s.Y < 0 Then
                        startLocation.Y = endLocation.Y
                    ElseIf s.Y = 0 Then
                        s.Y = 1
                    End If
    
    
                    s.X = Math.Abs(s.X)
                    s.Y = Math.Abs(s.Y)
    
    
                    Dim b As Brush
                    b = New Drawing2D.LinearGradientBrush(New Rectangle(startLocation, s), CurrentColor, CurrentColor2, Drawing2D.LinearGradientMode.BackwardDiagonal)
                    g.FillRectangle(b, New Rectangle(startLocation, s))
    
    
                ElseIf CircleRadioButton.Checked Then
    
    
                    endLocation = e.Location
                    Dim s As Point
    
    
                    s.X = endLocation.X - startLocation.X
    
    
                    If s.X < 0 Then
                        startLocation.X = endLocation.X
    
    
                    End If
    
    
                    s.Y = endLocation.Y - startLocation.Y
    
    
                    If s.Y < 0 Then
                        startLocation.Y = endLocation.Y
    
    
                    End If
    
    
                    s.X = Math.Abs(s.X)
                    s.Y = Math.Abs(s.Y)
    
    
                    If s.X > s.Y Then
                        s.Y = s.X
                    Else
                        s.X = s.Y
                    End If
    
    
                    g.DrawEllipse(PenPoint, New Rectangle(startLocation, s))
    
    
                ElseIf ArcRadioButton.Checked Then
                    endLocation = e.Location
    
    
                    Dim s As Point
    
    
                    s.X = endLocation.X - startLocation.X
    
    
                    If s.X < 0 Then
                        startLocation.X = endLocation.X
    
    
                    End If
    
    
                    s.Y = endLocation.Y - startLocation.Y
    
    
                    If s.Y < 0 Then
                        startLocation.Y = endLocation.Y
    
    
                    End If
    
    
                    s.X = Math.Abs(s.X)
                    s.Y = Math.Abs(s.Y)
    
    
                    If s.X > s.Y Then
                        s.Y = s.X
                    Else
                        s.X = s.Y
                    End If
    
    
                    g.DrawArc(PenPoint, New Rectangle(startLocation, s), 0, -180)
    
    
                ElseIf ParallelepipedRadioButton.Checked Then
    
    
                    endLocation = e.Location
    
    
                    Dim s As Point
    
    
                    s.X = endLocation.X - startLocation.X
    
    
                    If s.X < 0 Then
                        Dim tmp As Integer = startLocation.X
                        startLocation.X = endLocation.X
                        endLocation.X = tmp
                    End If
    
    
                    s.Y = endLocation.Y - startLocation.Y
    
    
                    If s.Y < 0 Then
                        Dim tmp As Integer = startLocation.Y
                        startLocation.Y = endLocation.Y
                        endLocation.Y = tmp
                    End If
    
    
                    s.X = Math.Abs(s.X)
                    s.Y = Math.Abs(s.Y)
    
    
    
    
    
    
                    Dim p(3) As Point
    
    
                    p(0) = New Point(startLocation.X + s.X / 5, startLocation.Y)
                    p(1) = New Point(startLocation.X + s.X, startLocation.Y)
    
    
                    p(2) = New Point(endLocation.X - s.X / 5, endLocation.Y)
                    p(3) = New Point(endLocation.X - s.X, endLocation.Y)
    
    
                    g.DrawPolygon(PenPoint, p)
    
    
                ElseIf FillRadioButton.Checked Then
                    FillRegion(e.X, e.Y, CurrentColor)
    
    
                ElseIf TextRadioButton.Checked Then
                    Dim txt As String = Me.TextDrawTextBox.Text
                    g.DrawString(txt, CurrentFont, New Drawing2D.HatchBrush(Drawing2D.HatchStyle.BackwardDiagonal, CurrentColor), e.X, e.Y)
    
    
                End If
    
    
            End If
    
    
            drawing = False
    
    
            UpdateImage()
    
    
        End Sub
    Code:
    Private Sub PaintForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
            g = Graphics.FromImage(LastImage)
            g.Clear(Color.White)
            UpdateImage()
            ReloadPen(PenWidth, CurrentColor)
    
    
        End Sub

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,501

    Re: Smoothing the Pen

    I don't know how to improve it either but it's obvious why it's happening as you're not drawing a continuous line but a series of, sometimes very short, straight lines. The wider the stroke the more obvious it becomes that this is effectively a series of rectangles at different angles to each other which means there simply has to be white space between them at one end or the other.

  3. #3
    Junior Member
    Join Date
    Aug 12
    Posts
    20

    Re: Smoothing the Pen

    There are a lot of same program that has the same problem. I can't find solution about this.

  4. #4
    vb Coda .paul.'s Avatar
    Join Date
    May 07
    Location
    Chelmsford UK
    Posts
    16,471

    Re: Smoothing the Pen

    try this:

    vb.net Code:
    1. Public Class Form1
    2.     Dim multistartPos As New List(Of Point)
    3.  
    4.     Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    5.         If e.Button = Windows.Forms.MouseButtons.Left Then
    6.             multistartPos.Add(New Point(e.X, e.Y))
    7.         End If
    8.     End Sub
    9.    
    10.     Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    11.         If multistartPos.Count > 0 Then
    12.             Dim gr As Graphics = Graphics.FromHwnd(PictureBox1.Handle)
    13.             multistartPos.Add(New Point(e.X, e.Y))
    14.             If e.Button = Windows.Forms.MouseButtons.Left Then
    15.                 gr.DrawLines(New Pen(Color.Black, 5), multistartPos.ToArray)
    16.             End If
    17.         End If
    18.     End Sub
    19.  
    20.     Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    21.         multistartPos.Clear()
    22.     End Sub
    23.  
    24. End Class

Posting Permissions

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