4 Replies - 1455 Views - Last Post: 19 August 2012 - 07:37 PM Rate Topic: -----

#1 k3nnt0ter0   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 188
  • Joined: 16-January 10

Smoothing the Pen

Posted 19 August 2012 - 08:58 AM

I attached the program so that you could try it yourselves and the code is quite long.

This program enables you to draw in free hand. When you enlarge the size of the pen, it will have a crack/gap. (See the image)
Posted Image

Is it possible to make it smoother like the one in MS Paint? How do I make it smooth?

Attached File(s)


This post has been edited by k3nnt0ter0: 19 August 2012 - 08:59 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Smoothing the Pen

#2 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Smoothing the Pen

Posted 19 August 2012 - 10:36 AM

Depends on how you are doing it. If you using points then use the DrawCurve method. Sorry I don't to download projects. Just post the mousedown/move/up events and the paint event if you are still having trouble.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14417
  • View blog
  • Posts: 57,803
  • Joined: 12-June 08

Re: Smoothing the Pen

Posted 19 August 2012 - 10:36 AM

Please post the relevant code for you doing the actual pen drawing..
Was This Post Helpful? 0
  • +
  • -

#4 k3nnt0ter0   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 188
  • Joined: 16-January 10

Re: Smoothing the Pen

Posted 19 August 2012 - 07:02 PM

Here it is. I reduce some non related codes. Hope you could help me out. I really need to fix that.

Public Class PaintForm
Dim PenWidth As Single = 1.0F
    Dim PenPoint As Pen

 Sub ReloadPen(ByVal PenWd As Single, ByVal CurColor As Color)
        PenPoint = New Pen(CurColor, PenWd)

    End Sub

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

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

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

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

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14417
  • View blog
  • Posts: 57,803
  • Joined: 12-June 08

Re: Smoothing the Pen

Posted 19 August 2012 - 07:37 PM

Yeah I can see how that would be pretty damn inefficient and backup your events.

What you should be doing is taking the drawing out of the mouse events and keep that with the form's paint events.. the mouse events just track where the mouse is and kick off an invalidate/repaint for the form. The form's paint event then would take the current mouse location and do a draw.

Here - I did something similar a while back in a tutorial about drawing the 'rubber band'/marquee box on a form.

http://www.dreaminco...bberband-shape/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1