【问题标题】:Remove Or Delete the Rectangle drawn on the PictureBox删除或删除在 PictureBox 上绘制的 Rectangle
【发布时间】:2020-07-29 01:06:50
【问题描述】:

我目前正在解决一个错误,该错误将删除 PictureBox 上创建的矩形。问题是当我单击 PictureBox 上的一个项目并调整窗口窗体大小时,矩形不会随着所选项目继续移动。这是创建矩形的代码:

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

在调整窗体大小后,我想删除在图片框上创建一个矩形。

我尝试了这个解决方案,但矩形仍在 PictureBox 中。

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

但是不行,矩形还在图片框中。

【问题讨论】:

  • 问题是你没有在PictureBox 上绘制任何东西。您正在绘制 Image 并在 PictureBox 中显示它。他们是两个不同的东西。您在Image 上绘制的任何内容都将永久保存在Image 上。如果那不是您想要的,那么请停止使用Image。如果您实际上在PictureBox 本身上绘制,您将在其Paint 事件处理程序中执行此操作,那么所有内容都会在每个事件上重新绘制,因此更改绘图很简单。决定你想要什么,如果你想要后者,就改变你的绘画方式。
  • @jmcilhinney 您提到它是永久的先生,因为我在图像上绘制它。有什么办法可以撤销吗?
  • 你明白“永久”是什么意思吗?如果有办法撤消它,那么它就不会是永久的,所以我不会说它是永久的。位图中的每个像素只有一个值。当您在该位图上绘制某些东西时,您会更改这些像素的值。没有记忆以前的值是什么,所以没有什么可以撤消的。您始终可以保留原始Image 的副本并重新开始,但这并不能撤消任何事情,我认为这也不需要解释。除非有特定原因要在 Image 上绘图,否则请在控件上绘图。
  • 查看简单示例here。重要的是笔记。查看形状是如何绘制的,如何在按住左键拖动鼠标时存储和更新鼠标位置,以及如何将已经绘制的形状存储在一组对象中,然后可以使用这些对象来跟踪每个形状并删除一个或需要时从收藏中获取更多内容。
  • @Jimi 谢谢先生,我会用它作为参考。

标签: vb.net picturebox


【解决方案1】:

这是一个简单的例子,展示了 PictureBox 的 Paint() 事件被用来绘制一个可以移动和打开/关闭的矩形:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多