为什么画点有时消失?

问题描述 投票:-1回答:1

我尝试在图像上画点…

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            PictureBox1.Image = Image.FromFile("C:\Users\SHEMMY-7X64\Pictures\postage.jpg")
            Using p As New System.Drawing.Pen(Color.Yellow, 4)
                Using g As Graphics = PictureBox1.CreateGraphics()
                    g.DrawEllipse(p, 15, 5, 10, 10)
                End Using
            End Using
        End Sub

图像被绘制,但点未绘制。将代码分为2个步骤时:1.加载图像

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        PictureBox1.Image = Image.FromFile("C:\Users\SHEMMY-7X64\Pictures\postage.jpg")
    End Sub

2。油漆

 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Using p As New System.Drawing.Pen(Color.Yellow, 4)
            Using g As Graphics = PictureBox1.CreateGraphics()
                g.DrawEllipse(p, 15, 5, 10, 10)
            End Using
        End Using

    End Sub

这次点画了。我在另一个站点上发布了这个问题,并被告知这是时间问题。

好吧,这是时间的问题,但是如何解决?

vb.net paint
1个回答
0
投票

您需要绘制包括图像在内的所有内容。您可以:

创建Bitmap类型的类级别变量,并将其命名为Bmp:

Private Bmp as Bitmap

要加载新图像:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'dispose the old image if any:
    bmp?.Dispose()

    'and assing the new one:
    bmp = New Bitmap(Image.FromFile("C:\Users\SHEMMY-7X64\Pictures\postage.jpg"))

    'and call:
    PictureBox1.Invalidate()
End Sub

现在是绘画程序:

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    If bmp IsNot Nothing Then
        Dim srcRect As New Rectangle(0, 0, bmp.Width, bmp.Height)
        Dim desRect As New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Height)
        Dim G As Graphics = e.Graphics

        G.SmoothingMode = SmoothingMode.AntiAlias

        G.Clear(BackColor) 'or: G.Clear(Parent.Backcolor) if you want.
        G.DrawImage(bmp, desRect, srcRect, GraphicsUnit.Pixel)

        Using pn As New Pen(Color.Yellow, 4)
            G.DrawEllipse(pn, 15, 5, 10, 10)
        End Using
    End If
End Sub

最后,不要忘了清理:

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    bmp?.Dispose()
End Sub

祝你好运。>>


0
投票

我尝试使用喘气事件,它运行良好。谢谢JQSOFT

© www.soinside.com 2019 - 2024. All rights reserved.