我为图片编辑器创建CTRL + Z undo
功能,每次点击屏幕都会使用以下代码保存图片:
Private Sub SaveMap(ByVal sender As Object, ByVal e As EventArgs) Handles PictureBox1.Click
Dim asd As New Bitmap(Form6.PictureBox1.Image)
GlobalVariables.BackUp.Add(Form6.PictureBox1.Image)
asd.Dispose()
End Sub
每次用户按CTRL + Z
时,都会执行此代码
Private Sub Picturebox1_KeyPress(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Z And e.Control Then
GlobalVariables.BackUp.RemoveAt(GlobalVariables.BackUp.Count - 1)
Form6.PictureBox1.Image = New Bitmap(GlobalVariables.BackUp.Last)
Form6.PictureBox1.Refresh()
End If
End Sub
但请注意,Public Shared BackUp As New List(Of Bitmap)
中保存的所有位图都是相同的。
因为无论我使用GlobalVariables.BackUp.Item()
返回哪个索引,每个索引都返回完全相同的图像。
为什么列表中的位图会被替换,而不是一个接一个地添加?
答案 0 :(得分:0)
您没有保存新的位图,而是保存对picturebox.image的引用。作为参考,它会在PictureBox.Image
更改时更改。这应该通过克隆当前显示的内容将新图像保存到列表中:
Private Sub SaveMap(ByVal sender ...
Dim bmp As Bitmap
bmp = Form6.PictureBox1.Image.Clone ' clone the image
GlobalVariables.BackUp.Add(bmp) ' save the copy
' dispose of it later when you remove it from the list
End Sub