实现undo / redo时出现问题

时间:2009-05-14 11:33:26

标签: vb.net gdi undo-redo

我的代码存在问题

点击撤消后我无法在图像上绘图,我不知道为什么

这是我的代码

Imports System.Drawing.Graphics
Imports System.Drawing.Bitmap
Imports System.Drawing
Public Class FrmChild
    Dim Xstart As Short
    Dim Ystart As Short
    Dim Xend As Short
    Dim Yend As Short
    Dim BoolErasing As Boolean = False
    Dim BoolDrawing As Boolean = False
    Dim Image As New Bitmap(1500, 1200)
    Public GraphFun As Graphics = Graphics.FromImage(Image)
    Dim ErasingPen As New Pen(Drawing.Color.White, 3)
    Dim DrawingPen As New Pen(Drawing.Color.Black, 3)
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        undo.Push(Image.Clone())
        redo.Clear()
        btnredo.Enabled = False
        If Not btnundo.Enabled Then btnundo.Enabled = True
        Xstart = e.X
        Ystart = e.Y
        If btnEraser Then
            BoolErasing = True
        ElseIf btnPencil Then
            BoolDrawing = True
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If BoolErasing Then
            GraphFun.DrawLine(ErasingPen, Xstart, Ystart, e.X, e.Y)
        ElseIf BoolDrawing Then
            GraphFun.DrawLine(DrawingPen, Xstart, Ystart, e.X, e.Y)
        End If
        Xstart = e.X
        Ystart = e.Y
        PictureBox1.Image = Image
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        Xend = e.X
        Yend = e.Y
        BoolErasing = False
        BoolDrawing = False
        PictureBox1.Image = Image
    End Sub


    Private Sub Btnundo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnundo.Click
        If Not btnredo.Enabled Then btnredo.Enabled = True
        redo.Push(Image.Clone())
        Image = undo.Pop()
        PictureBox1.Image = Image
        If undo.Count = 0 Then
            btnundo.Enabled = False
        End If
    End Sub


    Private Sub FrmChild_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnundo.Enabled = False
        btnredo.Enabled = False
    End Sub

    Private Sub btnredo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnredo.Click
        If Not btnundo.Enabled Then btnundo.Enabled = True
        undo.Push(Image.Clone())
        Image = redo.Pop()
        PictureBox1.Image = Image
        If redo.Count = 0 Then btnredo.Enabled = False
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

那是因为您正在为特定的Bitmap对象创建Graphics对象。您只能使用它来绘制特定的位图,当您使用撤消中的位图替换位图时,您仍在绘制相同的位图,但不再显示该位图。

而不是创建一个Graphics对象并保持它,在需要时为当前位图创建一个,然后再处理它:

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
   Using graphFun As Graphics = Graphics.FromImage(Image)
      If BoolErasing Then
         GraphFun.DrawLine(ErasingPen, Xstart, Ystart, e.X, e.Y)
      ElseIf BoolDrawing Then
         GraphFun.DrawLine(DrawingPen, Xstart, Ystart, e.X, e.Y)
      End If
   End Using
   Xstart = e.X
   Ystart = e.Y
   PictureBox1.Image = Image
End Sub