+ Reply to Thread
Results 1 to 3 of 3

Thread: Drawing on PictureBox

  1. #1
    yuzhen is offline VB.NET Forum Newbie yuzhen is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    May 2009
    Posts
    12
    Reputation
    19

    Exclamation Drawing on PictureBox

    Hi,

    Currently I'm trying to make multi selection (draw shape) on the picturebox but no matter how i edit my code, it jus dn draw any square or circle when I click on my button (either circle or square).

    Can some1 pls guide me on how I can get it work.
    Below are the codes that i'm currently working on:

    Code:
    Public Class Page_2
    
        Public blnCircleClicked As Boolean 'Is The Circle Tool Clicked?
        Public blnSquareClicked As Boolean 'Is The Square Tool Clicked?
    
        Public cColor As Color 'Selected Color To Draw With
    
        'Declare Starting Points For Drawn Objects
        Private sStartX As Short
        Private sStartY As Short
    
        'Declare Ending points For Drawn Objects
        Private sEndX As Short
        Private sEndY As Short
    
        Private Sub btnCircle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCircle.Click
    
            'Set All Boolean Flags Of Tools Click To False, Except For The Current One : Circle
            blnSquareClicked = False
            blnCircleClicked = True
    
            'Refresh / Repaint The Buttons, To Indicate Current Selection State
            btnSquare.Refresh()
            btnCircle.Refresh()
    
    
        End Sub
    
       Private Sub btnCircle_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles btnCircle.Paint
    
            'Declare A GraphicsPath Object, Which Is Used To Draw The Shape Of The Button
            Dim CirclePath As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath
    
            'Create A 60 x 60 Circle Path
            CirclePath.AddEllipse(New Rectangle(0, 0, 30, 30))
    
            'Size Of The Button
            btnCircle.Size = New System.Drawing.Size(30, 30)
    
            If blnCircleClicked Then
                'If The Button Is Selected To Draw, Change The Color
                btnCircle.BackColor = Color.Blue
            Else
                'If The Button Is Not Selected To Draw With, Change Back To Original Color
                btnCircle.BackColor = Color.Black
            End If
    
            'Create The Circular Shaped Button, Based On The Graphics Path
            btnCircle.Region = New Region(CirclePath)
    
            'Release All Resources Owned By The Graphics Path Object
            CirclePath.Dispose()
    
        End Sub
    
        Private Sub btnSquare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquare.Click
    
            'Set All Boolean Flags Of Tools Click To False, Except For The Current One : Square
            blnSquareClicked = True
            blnCircleClicked = False
    
            'Refresh / Repaint The Buttons, To Indicate Current Selection State
            btnSquare.Refresh()
            btnCircle.Refresh()
    
        End Sub
    
        Private Sub btnSquare_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles btnSquare.Paint
    
            'Declare A GraphicsPath Object, Which Is Used To Draw The Shape Of The Button
            Dim SquarePath As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath
    
            'Create A 30 x 30 Square Path
            SquarePath.AddRectangle(New Rectangle(0, 0, 30, 30))
    
            'Size Of The Button
            btnSquare.Size = New System.Drawing.Size(30, 30)
    
            If blnSquareClicked Then
                'If The Button Is Selected To Draw, Change The Color
                btnSquare.BackColor = Color.Blue
            Else
                'If The Button Is Not Selected To Draw With, Change Back To Original Color
                btnSquare.BackColor = Color.Black
            End If
    
            'Create The Square Shaped Button, Based On The Graphics Path
            btnSquare.Region = New Region(SquarePath)
    
            'Release All Resources Owned By The Graphics Path Object
            SquarePath.Dispose()
    
        End Sub
    
       Private Sub pbBody_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseDown
    
            'Initialise Starting Points Of Shape, Once Mouse Button Is Pressed Down
            sStartX = e.X
            sStartY = e.Y
    
        End Sub
    
        Private Sub pbBody_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbBody.MouseUp
    
            'Create And Initialise Pens To Draw The Particular Outline Shapes With.  Color : Black, Width : 3
            Dim pCirclePen As New Pen(Color.Black, 3)
            Dim pSquarePen As New Pen(Color.Black, 3)
    
            'Create And Initialise Brushes To Fill The Particular Shapes With.  Color : Black
            Dim sbCircleBrush As New SolidBrush(Color.Black)
            Dim sbSquareBrush As New SolidBrush(Color.Black)
    
            'Initialise Ending Points Of Shape, Once Mouse Button Is Released
            sEndX = e.X
            sEndY = e.Y
    
            'Set The Images Drawn Thus Far In The Picture Box = To The In - Memory Image Object
            'Me.pbBody.Image = bImage
    
    
            'Determine If The Circle Tool Has Been Clicked
            If blnCircleClicked Then
                'Yes, It Has Been Clicked, Set The Pen's Color To Selected Color
                pCirclePen.Color = cColor
    
                'Draw The Circle With The Current Starting, And Ending Values
                pbBody.CreateGraphics.DrawEllipse(pCirclePen, sStartX, sStartY, sEndX - sStartX, sEndY - sStartY)
    
            End If
    
            'Determine If The Square Tool Has Been Clicked
            If blnSquareClicked Then
                'Yes, It Has Been Clicked, Set The Pen's Color To Selected Color
                pSquarePen.Color = cColor
    
                'Draw The Square With The Current Starting, And Ending Values
    
                Dim SquareX As Integer = Math.Min(sStartX, sEndX)
                Dim SquareY As Integer = Math.Min(sStartY, sEndY)
                Dim SquareWidth As Integer = Math.Abs(sStartX - sEndX)
                Dim SquareHeight As Integer = Math.Abs(sStartY - sEndY)
    
                pbBody.CreateGraphics.DrawRectangle(pSquarePen, SquareX, SquareY, SquareWidth, SquareHeight)
    
            End If
    
            'Dispose Of All Pens
            pCirclePen.Dispose()
            pSquarePen.Dispose()
    
            'Dispose Of All Brushes
            sbCircleBrush.Dispose()
            sbSquareBrush.Dispose()
    
        End Sub
    I've attached the printscreen for your reference. And are those codes in GREEN needed? because I dont see any different when I put it in.
    Overall my question are..... 1) how can i modify the codes so that it is able to able on picturebox? 2) Are those codes in GREEN necessary?

    Thank you
    Attached Images

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    41
    Posts
    6,907
    Reputation
    670

    Default

    You're supposed to be handling the Paint event of the PictureBox, not the Buttons. Take a look at [ame="http://www.vbforums.com/showthread.php?t=426684"]this[/ame].

    Also, I suggest that you don't use Button controls there but rather RadioButtons. You can set the Appearance property to Button so that they look just like regular Buttons, but they will behave like RadioButtons in that only one will be depressed at a time and clicking one will leave it depressed until another is clicked. You test the Checked property just as you do with any other RadioButtons to see which is depressed. There's no point having two Boolean variables because as soon as you test one you know for a fact the value of the other.

  3. #3
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute jmcilhinney has a reputation beyond repute
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Age
    41
    Posts
    6,907
    Reputation
    670

    Default

    Also, please post in the most appropriate forum for the topic, not just the first one you come to. Thread moved.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

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