vb.netpicturebox

Remove Or Delete the Rectangle drawn on the PictureBox


I am currently solving a bug that will remove the created rectangle on the PictureBox. The problem is that when I click an Item on the PictureBox and Resize the windows form, the rectangle does not move on with the item selected. This is the code creating the rectangle:

Private Sub paintRectangle(pictBox As System.Windows.Forms.PictureBox, pic As Image)
    If pic Is Nothing Then Exit Sub

    pictBox.Image = pic

    If m_rect_x = -1 And m_rect_y = -1 Then
        Return
    End If

    Dim graphic As System.Drawing.Graphics
    Dim redselpen As System.Drawing.Pen
    Dim yNegative As Integer = 3    

    redselpen = New System.Drawing.Pen(Color.Blue)

    redselpen.DashStyle = Drawing2D.DashStyle.DashDot
    If pictBox.Image IsNot Nothing Then
        graphic = System.Drawing.Graphics.FromImage(pictBox.Image)
        graphic.DrawRectangle(redselpen, m_rect_x, m_rect_y - yNegative, SystemConfig.iRectWidth, SystemConfig.iRectHeight + 2)
        pictBox.Image = pictBox.Image
    End If
End Sub

After Resizing the Form, I want to remove the create a rectangle on the PictureBox.

I tried this solution but the Rectangle is still in the PictureBox.

How to remove all the drawn rectangles on the picture box? (Not on the image)

But it does not work, the rectangle is still in the picturebox.


Solution

  • Here's a simple example showing the Paint() event of a PictureBox being used to draw a rectangle that can be moved and turned on/off:

    enter image description here

    Public Class Form1
    
        Private yNegative As Integer = 3
        Private pt As New Nullable(Of Point)
        Private drawRectangle As Boolean = False
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            If drawRectangle AndAlso pt.HasValue Then
                Using redselpen As New System.Drawing.Pen(Color.Blue)
                    redselpen.DashStyle = Drawing2D.DashStyle.DashDot
                    e.Graphics.DrawRectangle(redselpen, pt.Value.X, pt.Value.Y - yNegative, SystemConfig.iRectWidth, SystemConfig.iRectHeight + 2)
                End Using
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            pt = New Point(25, 25)
            drawRectangle = True
            PictureBox1.Invalidate()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            drawRectangle = Not drawRectangle ' toggle the rectangle on/off
            PictureBox1.Invalidate()
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            pt = New Point(150, 25)
            drawRectangle = True
            PictureBox1.Invalidate()
        End Sub
    
    End Class