none
Draw & Remove selected shapes in Picture box RRS feed

  • Question

  • Hi,

     I am drawing shape controls in Picture box. Now I have one problem based on remove/erase selected shapes in the picture box.

    In my project, many shapes are drawing in picture box, now I want to delete selected shape only.

    Currently I am trying  clear picture box, again redraw all object, then select current object. Any other way  please help

    Thursday, March 19, 2020 8:18 PM

Answers

  • Hi

    Here is some starting code.

    This is a stand alone example, needs a Form1 with a ToolStrip1 containing Buttons1-3 and PictureBox1.

    Start a new project and copy/replace this code over the default Form1 code after you add the controls mentioned in the Designer.

    When you run it, click one or more of the ToolStrip buttons - they toggle the shapes on/off.

    This code is to get you started and is much smaller and understandable than the larger example you posted. There are only 3 shapes illustrated, but as they are all GraphicsPath objects, there is no limit to the complexity of the shapes you can design.

    Option Strict On
    Option Explicit On
    Imports System.Drawing.Drawing2D
    Public Class Form1
      Dim Shapes As New List(Of Shape)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Shapes.Add(New Shape With {.GP = Line(20, 40, 260, 200), .col = Color.Red, .Vis = True, .thickness = 24})
        Shapes.Add(New Shape With {.GP = Rect(100, 60, 150, 200), .col = Color.Blue, .Vis = True, .thickness = 3})
        Shapes.Add(New Shape With {.GP = Circ(170, 180, 60, 60), .col = Color.Purple, .Vis = True, .thickness = 10})
      End Sub
      Function Line(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer) As GraphicsPath
        Dim gp As New GraphicsPath
        gp.AddLine(x1, y1, x2, y2)
        Return gp
      End Function
      Function Rect(x As Integer, y As Integer, w As Integer, h As Integer) As GraphicsPath
        Dim gp As New GraphicsPath
        gp.AddRectangle(New Rectangle(x, y, w, h))
        Return gp
      End Function
      Function Circ(x As Integer, y As Integer, w As Integer, h As Integer) As GraphicsPath
        Dim gp As New GraphicsPath
        gp.AddEllipse(New Rectangle(x, y, w, h))
        Return gp
      End Function
      Class Shape
        Property GP As GraphicsPath
        Property col As Color
        Property thickness As Single
        Property Vis As Boolean
      End Class
      Private Sub picCanvas_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        For Each sh As Shape In Shapes
          If sh.Vis Then
            e.Graphics.DrawPath(New Pen(sh.col, sh.thickness), sh.GP)
          End If
        Next
      End Sub
    
      Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        Shapes(0).Vis = Not Shapes(0).Vis
        picturebox1.Invalidate()
      End Sub
      Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
        Shapes(1).Vis = Not Shapes(1).Vis
        PictureBox1.Invalidate()
      End Sub
      Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
        Shapes(2).Vis = Not Shapes(2).Vis
        PictureBox1.Invalidate()
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, March 20, 2020 12:48 AM
    • Marked as answer by J_97 Wednesday, March 25, 2020 1:06 PM
    Friday, March 20, 2020 12:10 AM

All replies

  • Hi

    If you maintain the ID's of the shapes you are drawing (perhaps in a .Tag property), then in the Paint event handler where I assume you are drawing the shapes, check the .Tag and if True then draw it else ignore it and therefor it will ;disappear' from the Picturebox.

    So all you meed to do is mark each shape .Tag property with True or False for a shape to be drawn or not.

    Since you didn't paste any code, I can't help any further than that.


    Regards Les, Livingston, Scotland

    Thursday, March 19, 2020 9:07 PM
  • Hi Ieshay,

    can you share or give the example source code? better you give the link where i can download it.
    please. I am student and newbie in vb .net. :)

    Thursday, March 19, 2020 9:47 PM
  • Hi

    You need to post your code before asking me to post mine!


    Regards Les, Livingston, Scotland

    Thursday, March 19, 2020 10:12 PM
  • Here is my code. this project have background grid line and vertical line right and left. 

    it is also have different shapes which are can drawing. the problem is how i can delete selected shapes? please

    Public Class Form1
        Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
            Me.Close()
        End Sub
    
    
        ' True while we are drawing the new line.
        Private m_Drawing As Boolean
        Dim drawhor As Boolean
        Dim drawver As Boolean
        Dim drawNo As Boolean
        Dim drawNc As Boolean
        Dim drawOut As Boolean
    
        'labelA properties
        Dim labelA As New Label
    
    
        ' Buffer for erasing rubberband lines.
        Private m_BufferBitmap As Bitmap
        Private m_BufferGraphics As Graphics
        Private lineGraphics As Graphics
    
        ' The mouse position.
        Private m_X1 As Integer
        Private m_Y1 As Integer
        Private m_X2 As Integer
        Private m_Y2 As Integer
    
        Private Sub SaveSnapshot()
            Dim new_bitmap As Bitmap
    
            ' Make a new bitmap that fits the PictureBox.
            new_bitmap = New Bitmap(picCanvas.Size.Width, picCanvas.Size.Height)
            m_BufferGraphics = Graphics.FromImage(new_bitmap)
    
            ' Clear the new bitmap.
            m_BufferGraphics.Clear(picCanvas.BackColor)
    
            ' Draw the positioning grid.
            DrawGrid(new_bitmap)
            DrawGridY(new_bitmap)
    
    
            ' Copy the existing bitmap's contents into
            ' the new bitmap.
            If Not (m_BufferBitmap Is Nothing) Then
                m_BufferGraphics.DrawImage(m_BufferBitmap, 0, 0)
            End If
    
            ' Save the new bitmap and graphics objects.
            m_BufferBitmap = new_bitmap
        End Sub
    
        ' Redraw the saved buffer.
        Private Sub DrawPicture(ByVal gr As Graphics)
            If Not (m_BufferBitmap Is Nothing) Then gr.DrawImage(m_BufferBitmap, 0, 0)
        End Sub
    
        ' Draw the positioning grid.
        Private Sub DrawGrid(ByVal bm As Bitmap)
            For Y As Integer = 0 To bm.Width - 1 Step 10
                For X As Integer = 0 To bm.Height - 1 Step 30
                    bm.SetPixel(Y, X, Color.Black)
                Next X
            Next Y
    
            Dim gr As Graphics = Graphics.FromImage(bm)
            Dim p1 As New Pen(Color.Black, 7)
            gr.DrawLine(p1, 100, 100, 100, 800) 'draw vertical line left
            gr.DrawLine(p1, 800, 100, 800, 800) 'draw vertical line right
    
        End Sub
        Private Sub DrawGridY(ByVal bm As Bitmap)
            For Y As Integer = 0 To bm.Height - 1 Step 10
                For X As Integer = 0 To bm.Width - 1 Step 30
                    bm.SetPixel(X, Y, Color.Black)
                Next X
            Next Y
        End Sub
    
        ' Redraw the picture.
        Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picCanvas.Paint
            DrawPicture(e.Graphics)
    
        End Sub
    
        ' Make the initial grid.
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            SaveSnapshot()
            picCanvas.CreateGraphics()
            Me.Size = New Size(1225, 1051)
            Me.Location = New Point(100, 0)
        End Sub
    
        ' Make the new grid.
        Private Sub picCanvas_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles picCanvas.Resize
            SaveSnapshot()
        End Sub
    
    
    
        ' Start drawing a rubberband line.
        Private Sub picCanvas_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown
            ' Do nothing if this isn'tthe left mouse button.
            If e.Button <> MouseButtons.Left Then Exit Sub
            m_Drawing = True
    
            ' Save a snapshot of the form.
            SaveSnapshot()
    
            ' Save the current mouse position.
            m_X1 = e.X
            m_Y1 = e.Y
    
            m_X2 = m_X1
            m_Y2 = m_Y1
        End Sub
    
        ' Continue drawing the rubberband line.
        Private Sub picCanvas_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseMove
            ' Do nothing if we're not drawing.
            If Not m_Drawing Then Exit Sub
    
            ' Save the new point.
            m_X2 = e.X
            m_Y2 = e.Y
            'SnapToGrid(m_X2, m_Y2)
    
            ' Erase the previous line.
            Dim gr As Graphics = picCanvas.CreateGraphics()
            DrawPicture(gr)
    
    
        End Sub
    
        ' Finish drawing the new line.
        Private Sub picCanvas_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseUp
            ' Do nothing if we're not drawing.
            If Not m_Drawing Then Exit Sub
            m_Drawing = False
    
            ' Draw the new line permanently on the buffer.
            If drawhor = True Then
                m_BufferGraphics.DrawLine(Pens.Black, 100, m_Y1, m_X1, m_Y1)
    
                ' Redraw to show the new line.
                DrawPicture(picCanvas.CreateGraphics())
            End If
    
            If drawver = True Then
                m_BufferGraphics.DrawLine(Pens.Black, m_X1, m_Y1, m_X1, m_Y1 + 50)
    
                ' Redraw to show the new line.
                DrawPicture(picCanvas.CreateGraphics())
            End If
    
            If drawNo = True Then
                m_BufferGraphics.DrawLine(Pens.Black, 100, m_Y1, m_X1 - 10, m_Y1)
                m_BufferGraphics.DrawLine(Pens.Black, m_X1 - 10, m_Y1 - 20, m_X1 - 10, m_Y1 + 20)
                m_BufferGraphics.DrawLine(Pens.Black, m_X1 + 10, m_Y1 - 20, m_X1 + 10, m_Y1 + 20)
                m_BufferGraphics.DrawLine(Pens.Black, m_X1 + 10, m_Y1, m_X1 + 50, m_Y1)
    
                ' Redraw to show the new line.
                DrawPicture(picCanvas.CreateGraphics())
            End If
    
            If drawNc = True Then
                m_BufferGraphics.DrawLine(Pens.Black, m_X1 - 50, m_Y1, m_X1 - 10, m_Y1)
                m_BufferGraphics.DrawLine(Pens.Black, m_X1 - 10, m_Y1 - 20, m_X1 - 10, m_Y1 + 20)
                m_BufferGraphics.DrawLine(Pens.Black, m_X1 + 10, m_Y1 - 20, m_X1 + 10, m_Y1 + 20)
                m_BufferGraphics.DrawLine(Pens.Black, m_X1 + 10, m_Y1, m_X1 + 50, m_Y1)
                m_BufferGraphics.DrawLine(Pens.Black, m_X1 - 15, m_Y1 + 15, m_X1 + 15, m_Y1 - 15)
    
                ' Redraw to show the new line.
                DrawPicture(picCanvas.CreateGraphics())
            End If
    
            If drawOut = True Then
                m_BufferGraphics.DrawLine(Pens.Black, m_X1 - 50, m_Y1, m_X1 - 10, m_Y1)
                m_BufferGraphics.DrawArc(Pens.Black, m_X1 - 10, m_Y1 - 15, 30, 30, 90, 180)
                m_BufferGraphics.DrawArc(Pens.Black, m_X1 + 1, m_Y1 - 15, 30, 30, -90, 180)
                m_BufferGraphics.DrawLine(Pens.Black, m_X1 + 32, m_Y1, 800, m_Y1)
    
    
                ' Redraw to show the new line.
                DrawPicture(picCanvas.CreateGraphics())
            End If
        End Sub
    
    
        Private Sub ToolStripButton7_Click(sender As Object, e As EventArgs) Handles ToolStripButton7.Click
            drawhor = False
            drawver = False
            drawNo = True
            drawNc = False
            drawOut = False
    
        End Sub
    
        Private Sub ToolStripButton8_Click(sender As Object, e As EventArgs) Handles ToolStripButton8.Click
            drawver = False
            drawhor = False
            drawNo = False
            drawNc = True
            drawOut = False
        End Sub
    
        Private Sub ToolStripButton9_Click(sender As Object, e As EventArgs) Handles ToolStripButton9.Click
            drawver = False
            drawhor = True
            drawNo = False
            drawNc = False
            drawOut = False
        End Sub
    
        Private Sub ToolStripButton10_Click(sender As Object, e As EventArgs) Handles ToolStripButton10.Click
            drawver = True
            drawhor = False
            drawNo = False
            drawNc = False
            drawOut = False
        End Sub
    
        Private Sub ToolStripButton11_Click(sender As Object, e As EventArgs) Handles ToolStripButton11.Click
            drawver = False
            drawhor = False
            drawNo = False
            drawNc = False
            drawOut = True
        End Sub
    
        Private Sub ToolStripButton6_Click(sender As Object, e As EventArgs) Handles ToolStripButton6.Click
            drawver = False
            drawhor = False
            drawNo = False
            drawNc = False
            drawOut = False
        End Sub
    End class
    


    Thursday, March 19, 2020 10:31 PM
  • Hi

    Well, this is not your code, where did you copy it from?

    It is always better to 'start again' with your OWN code rather than try to  firstly read, secondly understand, thirdly edit other peoples code.

    Sure, take ideas and snippets, but to just copy/paste and then ask someone else to make the changes you want is likely to meet with derision! It is tantamount to treating the forum as a personal software writing service.


    Regards Les, Livingston, Scotland

    Thursday, March 19, 2020 11:30 PM
  • Hi

    Here is some starting code.

    This is a stand alone example, needs a Form1 with a ToolStrip1 containing Buttons1-3 and PictureBox1.

    Start a new project and copy/replace this code over the default Form1 code after you add the controls mentioned in the Designer.

    When you run it, click one or more of the ToolStrip buttons - they toggle the shapes on/off.

    This code is to get you started and is much smaller and understandable than the larger example you posted. There are only 3 shapes illustrated, but as they are all GraphicsPath objects, there is no limit to the complexity of the shapes you can design.

    Option Strict On
    Option Explicit On
    Imports System.Drawing.Drawing2D
    Public Class Form1
      Dim Shapes As New List(Of Shape)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Shapes.Add(New Shape With {.GP = Line(20, 40, 260, 200), .col = Color.Red, .Vis = True, .thickness = 24})
        Shapes.Add(New Shape With {.GP = Rect(100, 60, 150, 200), .col = Color.Blue, .Vis = True, .thickness = 3})
        Shapes.Add(New Shape With {.GP = Circ(170, 180, 60, 60), .col = Color.Purple, .Vis = True, .thickness = 10})
      End Sub
      Function Line(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer) As GraphicsPath
        Dim gp As New GraphicsPath
        gp.AddLine(x1, y1, x2, y2)
        Return gp
      End Function
      Function Rect(x As Integer, y As Integer, w As Integer, h As Integer) As GraphicsPath
        Dim gp As New GraphicsPath
        gp.AddRectangle(New Rectangle(x, y, w, h))
        Return gp
      End Function
      Function Circ(x As Integer, y As Integer, w As Integer, h As Integer) As GraphicsPath
        Dim gp As New GraphicsPath
        gp.AddEllipse(New Rectangle(x, y, w, h))
        Return gp
      End Function
      Class Shape
        Property GP As GraphicsPath
        Property col As Color
        Property thickness As Single
        Property Vis As Boolean
      End Class
      Private Sub picCanvas_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        For Each sh As Shape In Shapes
          If sh.Vis Then
            e.Graphics.DrawPath(New Pen(sh.col, sh.thickness), sh.GP)
          End If
        Next
      End Sub
    
      Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        Shapes(0).Vis = Not Shapes(0).Vis
        picturebox1.Invalidate()
      End Sub
      Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
        Shapes(1).Vis = Not Shapes(1).Vis
        PictureBox1.Invalidate()
      End Sub
      Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
        Shapes(2).Vis = Not Shapes(2).Vis
        PictureBox1.Invalidate()
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, March 20, 2020 12:48 AM
    • Marked as answer by J_97 Wednesday, March 25, 2020 1:06 PM
    Friday, March 20, 2020 12:10 AM

  • Well, this is not your code, where did you copy it from?

    The OP of this thread not only copied the code, but even the EXACT opening
    post from another person's thread:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/d603b052-22d9-4d81-9170-5b30005f08a0/draw-amp-remove-shapes-in-picture-box?forum=vbgeneral

    - Wayne

    Friday, March 20, 2020 1:04 AM

  • Well, this is not your code, where did you copy it from?

    The OP of this thread not only copied the code, but even the EXACT opening
    post from another person's thread:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/d603b052-22d9-4d81-9170-5b30005f08a0/draw-amp-remove-shapes-in-picture-box?forum=vbgeneral

    - Wayne

    Hi Wayne

    Well, for the OP - Ha Ha Ha  - that is the derision I spoke of.🤣

    Well spotted Wayne


    Regards Les, Livingston, Scotland

    Friday, March 20, 2020 1:49 AM
  • Hi leshay, 
    Thanks for the codes. It teach me the real of concept of how to draw and delete shapes.
    To be honest, I am sorry about my code. before this, i am just copy code the other code and adjust it based on my project. I want just learn this new stuff because i never learn about vb .net. I know it is derision.
    BTW, thanks again for teach me.
    Friday, March 20, 2020 2:19 PM