【问题标题】:PictureBox2.Image = PictureBox1.Image seems to be linking the boxes instead of copying the image over?PictureBox2.Image = PictureBox1.Image 似乎是链接框而不是复制图像?
【发布时间】:2019-01-13 11:33:34
【问题描述】:

问题

我有一个带有 2 个图片框的简单表单

我允许用户在 PictureBox1 上绘图 当我单击表单上的 Buttonn 时,我想在 PictureBox1 中捕获图像并将其存储在 PictureBox2

问题是,如果我添加以下行: PictureBox2.Image = PictureBox1.Image PictureBox1 的任何更新都会立即反映在 PictureBox2 中?!?

我只想在那个时刻及时捕捉 PictureBox1 中的图像,以便我可以用它来“撤消”

技术

它是使用 Visual Studio 2019 Preview 的 Visual Basic、.Net 4.7.2 中的 Windows 窗体应用程序

代码

Public Class Form1

    Dim drawMouseDown = False ' Set initial mouse state to not clicked
    Dim drawMyBrush As New Pen(Brushes.White, 20) 'Set up the Brush
    Public drawCanvas As New Bitmap(245, 352) 'Set up Bitmap Canvas

    Private Sub btn_Color_Yellow_Click(sender As Object, e As EventArgs) Handles btn_Color_Yellow.Click
        drawMyBrush.Brush = Brushes.Yellow
        drawMyBrush.Width = 20
    End Sub

    Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
        drawMouseDown = True
    End Sub

    Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
        drawMouseDown = False
    End Sub

    Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
        Dim g As Graphics = Graphics.FromImage(drawCanvas)
        Static coord As New Point
        If drawMouseDown Then
            g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
            drawMyBrush.StartCap = Drawing2D.LineCap.Round
            drawMyBrush.EndCap = Drawing2D.LineCap.Round
            g.DrawLine(drawMyBrush, coord.X, coord.Y, e.X, e.Y)
            g.Dispose()
            PictureBox1.Image = drawCanvas
            Me.Refresh()
        End If
        coord = e.Location
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PictureBox2.Image = PictureBox1.Image 'Why does this not just update the PicBox2 image once?!? (or only when the Button is clicked)
    End Sub

End Class

期待

Button1 被点击时,我希望 PictureBox2 包含 PictureBox1 图像,当我继续在 PictureBox1上绘图时> 我确实希望它在用户在另一个上绘图时不断更新 PictureBox2

【问题讨论】:

  • 克隆图像而不是分配参考。这是绘制 Graphics 对象的一种非常尴尬的方式。真的,真的很慢,顺便说一句。您应该使用 PictureBox 的 Paint 事件,在其表面绘制您需要绘制的形状,如果您想将所有内容保存在 Bitmap 中,请一次性绘制所有形状,然后将 Bitmap 保存到磁盘。
  • 你真的应该使用Option Strict On - 它让 Visual Studio 告诉你变量类型不匹配的地方等等。
  • 类似的问题,同样的问题:stackoverflow.com/a/45043490/3740093

标签: vb.net


【解决方案1】:

正如您所怀疑的,PictureBox2.Image = PictureBox1.Image 将前者作为对后者的引用。

你可以做的是clone图片:

PictureBox2.Image = DirectCast(PictureBox1.Image.Clone(), Image)

【讨论】:

  • 是的!这解决了这个问题。谢谢!这让我发疯了
  • 不客气 :) 请注意Jimi's comment 并找出“正确”的绘图方式。
【解决方案2】:

因为您将 PictureBox2 的 Image 属性引用到 PictureBox1.Image。因此,当它们都指向同一个引用时,对 PictureBox 图像属性中的任何一个的任何更改都会影响另一个。

为了避免这种情况,根据PictureBox1.Image新建一个Bitmap对象实例,并将其设置为PictureBox2.Image:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    PictureBox2.Image = New Bitmap(PictureBox1.Image)
End Sub

【讨论】:

  • 令人惊讶的简单修复! :)
  • 这不是修复,这是另一个问题!
猜你喜欢
  • 2014-08-01
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 1970-01-01
相关资源
最近更新 更多