VB.NET绘制应用程序

时间:2014-05-20 11:14:07

标签: vb.net winforms paint draw picturebox

我在申请中面临两个问题:

  • 撤消功能
  • 绘图部分

当我在图片框上画画时,画得很好,当 - 让我说要撤消动作时,它会撤消它但是当我点击图片框时它反应就像重做一样功能,所有图纸都会显示在图像上。

第二个问题是:我希望能够编辑图片,所以我通过点击列表视图项加载图像但由于我错过了图像而没有显示图像而是显示白色我可以画的背景。

贝娄是我正在使用的代码

    Imports Microsoft.VisualBasic.PowerPacks

Public Class Form1
    Public drawgraph, g As Graphics
    Private redoBuffer As New Stack(Of Image)()
    Private undoBuffer As New Stack(Of Image)()
    Dim color As Color
    Dim UndoStack As New Stack(Of Bitmap)()
    Dim xStart, yStart, xEnd, yEnd As Integer
    Public Drawbitmap As Bitmap
    Dim Drawgraphics As Graphics
    Dim myPen As New Pen(color.Black, 4)
    Dim myColor As Color = color.Black
    Dim myPenWidth As Integer
    Dim myBGColor As Color = color.White
    Dim Drawing As Boolean

    Private Sub drawMyline()

        PictureBox4.Image = Drawbitmap
        Drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        Drawgraphics.DrawLine(myPen, xStart, yStart, xEnd, yEnd)

    End Sub

    Private Sub PushUndo(ByVal b As Bitmap)
        UndoStack.Push(b)
    End Sub
    Private Function PopUndo() As Bitmap
        If UndoStack.Count = 0 Then
            Return Nothing
            Exit Function
        End If
        If UndoStack.Count > 0 Then
            Return UndoStack.Pop
        End If

    End Function



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Drawbitmap = New Bitmap(PictureBox4.Width, PictureBox4.Height)
        Drawgraphics = Graphics.FromImage(Drawbitmap)
        PictureBox4.Image = Drawbitmap
        Drawgraphics.Clear(color.White)
         myPenWidth = NumericUpDown1.Value
        xStart = -1
        yStart = -1
        Drawing = False

    End Sub

    Private Sub PictureBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox7.Click
        Dim bmp As Bitmap
        bmp = PopUndo()
        If bmp IsNot Nothing Then
            PictureBox4.Image = bmp
        End If

    End Sub
    Private Sub PictureBox4_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseDown
        Drawing = True
        PushUndo(PictureBox4.Image.Clone)
    End Sub

    Private Sub PictureBox4_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseUp
        Drawing = False

    End Sub

    Private Sub PictureBox4_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox4.MouseMove
        If Drawing Then
            xStart = e.X
            yStart = e.Y
            drawMyline()
        End If
        xEnd = e.X
        yEnd = e.Y
    End Sub
End Class

我尝试进行更改,但是我无法将想要的图像加载到picturebox4中并在其上绘图,它总是会加载白色背景,因为它的工作方式有效,直到再次点击picturebox4并且所有撤消图纸出现了。有人可以帮我解决这两个问题吗?

0 个答案:

没有答案