【发布时间】: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