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:

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 GREENGREEN 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

Recommended Answers

All 5 Replies

Hi,

I had manage to solved the problem of unable to draw on the picturebox. Now I'm able to draw but the problem is after I draw some selection, after a scrolling of up n down the page, the rectangle that i drew before the scroll are all gone... what went wrong with my codes??

Public Class Page_2

'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 btnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click

        Dim Response As New Page_1()
        Page_1.Show()
        Me.Hide()

    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 : 1
        Dim pPen As New Pen(Color.Black, 1)

        'Initialise Ending Points Of Shape, Once Mouse Button Is Released
        sEndX = e.X
        sEndY = e.Y

        '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(pPen, SquareX, SquareY, SquareWidth, SquareHeight)

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        'Refresh The Picturebox's Display
        pbBody.Refresh()

    End Sub
End Class

Below is the printscreen of what I've mention above. How do I deal with it?

Thks =)

I still having problem for the re-draw part. i have added the codes below to my application:

Private Sub pbBody_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbBody.Paint

        'Draw the Final rectangle, as a repaint was issued
        e.Graphics.DrawEllipse(pPen, sStartX, sStartY, _
                            sEndX - sStartX, sEndY - sStartY)

    End Sub

The above codes only save what is being drew the latest. which mean only the new circle that is being draw wont disappear when i scroll up & down whereby the remainin circle will disappear.

Teme64 > I had try the 2 website that u gave me in my other post "Saving drawing on picturebox to SQL database" but i don't really get the way on how they do it.

It is not possible to draw the circle on Picture box. Bcoz Picturebox is a container.So better you can use panel control with the same coding given by DAWNIE.

Save the Information of all shapes (Circles and Squares) in a data structure and redraw it in paint event of PictureBox.

The last code is not good and has a lot of errors and i do not know how to fix it.
I'm new for vb but i'm serious working on this project yuo can see how it's almost done.

Here this the picture but how to save this code using picture with rectangle what i drawed on the picture ?

This is what you can see and what i have for now.

http://i44.tinypic.com/ev7sjk.jpg

then i crop the image from picture box and when i draw rectangle on the picture is good but the problem is that it can't save the drawed picture with marked rectangle.

So how to fix this problem using the same code in above ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.