0

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.

Levesque Xylia
  • 349
  • 3
  • 16
  • The issue is that you're not drawing anything on the `PictureBox`. You're drawing on an `Image` and displaying that in the `PictureBox`. They are two different things. Anything you draw on an `Image` is permanent on that `Image`. If that's not what you want then stop drawing on the `Image`. If you actually draw on the `PictureBox` itself, which you would do in its `Paint` event handler, then everything gets redrawn on every event, so changing the drawing is simple. Make up your mind what you want and change the way you're drawing if you want the latter. – jmcilhinney Jul 29 '20 at 01:10
  • @jmcilhinney you mentioned that it was permanent sir because i am drawing it on the Image. Is there a way how to undo it? – Levesque Xylia Jul 29 '20 at 01:18
  • Do you understand what "permanent" means? If there was a way to undo it then it wouldn't be permanent, so I wouldn't have said that it was. Each pixel in a bitmap only has one value. When you draw something on that bitmap, you change the values of those pixels. There's no memory of what the previous values were so there's nothing to undo. You can always keep a copy of the original `Image` and start again, but that's not undoing anything and I don't think that it's something that should need to be explained either. Unless there's a specific reason to draw on the `Image`, draw on the control. – jmcilhinney Jul 29 '20 at 01:22
  • See the simple example [here](https://stackoverflow.com/a/53708936/7444103). What counts there are the notes. See how the shapes are painted, how to store and update the mouse location when dragging the mouse with the left button pressed and how to store the shapes already drawn in a collection of objects that can then be used to track each shape and remove one or more from the collection when needed. – Jimi Jul 29 '20 at 01:54
  • @Jimi Thank you Sir, I will use that as a reference. – Levesque Xylia Jul 29 '20 at 02:23

1 Answers1

2

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
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40