这是在表单上绘制图像的最佳方法吗?我有大部分的Java经验和VB.net的经验。在Java中有可以调用的repaint()吗?有一个Me.refresh()。当你有什么要添加到表单或绘制到表单时,这是你打算调用的。
类别:
Public Class Form1
Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' Create image.
Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
' Create Point for upper-left corner of image.
Dim ulCorner As New Point(50, 50)
' Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner)
Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
' Create Point for upper-left corner of image.
Dim ulCorner2 As New Point(150, 150)
' Draw image to screen.
e.Graphics.DrawImage(newImage2, ulCorner2)
End Sub
End Class
我在这里尝试了这段代码,只需一个按钮就可以点击事件
Public Class Form1
Dim i As Integer
Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
i = 0
' Create image.
Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
' Create Point for upper-left corner of image.
Dim ulCorner As New Point(50, 50)
' Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner)
Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
' Create Point for upper-left corner of image.
Dim ulCorner2 As New Point(150, 150)
' Draw image to screen.
e.Graphics.DrawImage(newImage2, ulCorner2)
If i = 5 Then
' Create image.
Dim newImage3 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
' Create Point for upper-left corner of image.
Dim ulCorner3 As New Point(250, 250)
' Draw image to screen.
e.Graphics.DrawImage(newImage3, ulCorner3)
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
i = 5
Me.Refresh()
End Sub
End Class
刚刚在这里尝试了这段代码:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
i = 5
Me.Invalidate()
End Sub
刚刚测试过的新类:
Public Class Form2
Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Dim newImage3 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Private bgImage As Bitmap
Private srcImages As Image()
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
Dim img As Bitmap = Me.bgImage
If (img Is Nothing) Then
Me.bgImage = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
img = Me.bgImage
End If
If (Not Me.srcImages Is Nothing) Then
End If
MyBase.OnPaint(e)
End Sub
Private Sub _Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.srcImages = New Image(3 - 1) {}
Me.srcImages(0) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Me.srcImages(1) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Me.srcImages(2) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
End Sub
Private Sub _Disposed(sender As Object, e As EventArgs) Handles MyBase.Disposed
If (Not Me.srcImages Is Nothing) Then
For Each img As Image In Me.srcImages
If (Not img Is Nothing) Then
img.Dispose()
End If
Next
Me.srcImages = Nothing
End If
If (Not Me.bgImage Is Nothing) Then
Me.bgImage.Dispose()
Me.bgImage = Nothing
End If
End Sub
Private Sub _Resize(sender As Object, e As EventArgs) Handles MyBase.SizeChanged, MyBase.Resize
If (Not Me.bgImage Is Nothing) Then
Me.bgImage.Dispose()
Me.bgImage = Nothing
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.Invalidate()
End Sub
End Class
答案 0 :(得分:4)
第一个片段没有显示需要任何帮助的证据。 Paint事件处理程序中的任何内容都不依赖于类的状态,因此没有理由强制重绘。默认绘画已经足够好了。
第二个片段通过人工i
变量执行。在这种情况下,您总是会调用Me.Invalidate()
,这会将表单的整个客户区域标记为需要重新绘制。然后,当没有更重要的事情需要完成时,UI最终会获得Paint事件。使用Me.Refresh()
也可以起作用,但是很苛刻,而且非常罕见。我想不出一个好的例子。
请注意Invalidate()如何重载,您可以只标记窗口的一部分以重新绘制。这是一种使绘画更有效的优化。
如上所述,您的程序在使用一段时间后可能会崩溃。您必须调用加载的图像的Dispose()方法。看一下VB.NET 使用语句。您需要预先加载图像,以便在绘画时不会发生,并且不会重复发生。强烈建议遵循Winforms编程教程或书籍以避免这类陷阱。
答案 1 :(得分:2)
调用Me.Invalidate()
会使控件的整个表面无效并导致重绘控件。
我还建议您不在每个绘制周期中加载图片。 您可以在表单加载事件中加载源图像,并在表单处理时将其销毁。
Public Class Form1
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
Dim img As Bitmap = Me.bgImage
If (img Is Nothing) Then
Me.bgImage = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
img = Me.bgImage
End If
'TODO: Draw and paint.
If (Not Me.srcImages Is Nothing) Then
End If
MyBase.OnPaint(e)
End Sub
Private Sub _Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.srcImages = New Image(3 - 1) {}
Me.srcImages(0) = Image.FromFile("...")
Me.srcImages(1) = Image.FromFile("...")
Me.srcImages(2) = Image.FromFile("...")
End Sub
Private Sub _Disposed(sender As Object, e As EventArgs) Handles MyBase.Disposed
If (Not Me.srcImages Is Nothing) Then
For Each img As Image In Me.srcImages
If (Not img Is Nothing) Then
img.Dispose()
End If
Next
Me.srcImages = Nothing
End If
If (Not Me.bgImage Is Nothing) Then
Me.bgImage.Dispose()
Me.bgImage = Nothing
End If
End Sub
Private Sub _Resize(sender As Object, e As EventArgs) Handles MyBase.SizeChanged, MyBase.Resize
If (Not Me.bgImage Is Nothing) Then
Me.bgImage.Dispose()
Me.bgImage = Nothing
End If
End Sub
Private bgImage As Bitmap
Private srcImages As Image()
End Class