CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2012
    Posts
    16

    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
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Smoothing the Pen

    Looks like you create a NEW POINT in each function...

    Also, could change the SCALE to make it smaller...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Smoothing the Pen

    These might be of help to you :

    Creating Your Own Drawing Application with Visual Basic .NET, Part 1
    http://www.codeguru.com/csharp/.net/...le.php/c13205/

    Creating Your Own Drawing Application with Visual Basic.NET, Part 2
    http://www.codeguru.com/csharp/.net/...le.php/c13207/

    Creating Your Own Drawing Application in Visual Basic.NET, Part 3
    http://www.codeguru.com/vb/gen/vb_gr...le.php/c13611/

    Creating Your Own Drawing Application in Visual Basic.NET, Part 4
    http://www.codeguru.com/vb/gen/vb_gr...cle.php/c14007

  4. #4
    Join Date
    Aug 2012
    Posts
    16

    Re: Smoothing the Pen

    I've checked that link before but the Pen isn't smooth.

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Smoothing the Pen

    Two things...

    where are you defining startlocation.. It needs to be either global Or local to the form. It also appears that startlocation is been set to current location before you can plot the line..

    Next i see that the Graphics object g is derived from lastimage, and you always calling Updateimage to put it into the picture box... where is that sub ???
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured