Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用VB.NET清除PictureBox中的图形_Vb.net_Visual Studio_Graphics_Drawing_Picturebox - Fatal编程技术网

无法使用VB.NET清除PictureBox中的图形

无法使用VB.NET清除PictureBox中的图形,vb.net,visual-studio,graphics,drawing,picturebox,Vb.net,Visual Studio,Graphics,Drawing,Picturebox,我使用以下代码在PictureBox1上绘制简单的徒手(画笔)图形。绘图很好,但无法永久清除我绘制的图纸。如果单击按钮1图形将被清除,但一旦我移动到PictureBox1上,所有旧图形(和PictureBox1图像)将再次出现。有什么建议吗 Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown If e.Button = Mo

我使用以下代码在PictureBox1上绘制简单的徒手(画笔)图形。绘图很好,但无法永久清除我绘制的图纸。如果单击按钮1图形将被清除,但一旦我移动到PictureBox1上,所有旧图形(和PictureBox1图像)将再次出现。有什么建议吗

  Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = MouseButtons.Left Then
            mousePath.StartFigure()
        End If
  End Sub

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
        '// slide annotations 
        If e.Button = MouseButtons.Left Then
               Try
            mousePath.AddLine(e.X, e.Y, e.X, e.Y)    'Add mouse coordiantes to mousePath
             Catch
             End Try
        End If
       PictureBox1.Invalidate()
    End Sub

 Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        '// slide annotations 
        Try
            '// drwaing options
            myUserColor = System.Drawing.Color.Red
            myAlpha = 255
            myPenWidth = 3
            CurrentPen = New Pen(myUserColor, myPenWidth)
            e.Graphics.DrawPath(CurrentPen, mousePath)
        Catch
        End Try
    End Sub

Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
        Dim g As Graphics
        g = PictureBox1.CreateGraphics()
        g.Clear(PictureBox1.BackColor)
        g.Dispose()
    End Sub

切勿调用
CreateGraphics
。始终在事件处理程序中绘制所有图形。您正在
单击事件处理程序中创建
图形
对象并清除该对象,但下次引发该事件时,在
绘制
事件处理程序中再次绘制图形有什么用


您需要做的是将表示图形的所有数据存储在一个或多个字段中,在需要更改图形时更新该数据,并在事件处理程序中使用该数据进行绘制。如果要清除图形,可以清除该数据,然后通过调用
Invalidate
强制重新绘制。在
Paint
事件处理程序中,您正在绘制一个
GraphicsPath
存储在
mousePath
字段中。这意味着,在
单击事件处理程序中,需要清除
GraphicsPath
,然后调用
Invalidate
。然后将提示一个
绘制
事件,该事件将首先清除现有图形,然后执行新操作。由于没有新的操作,它将保持清晰。

删除按钮中的所有内容1\u单击,只需添加并
Invalidate()
PictureBox即可。绘制贝塞尔曲线。