我无法找到将多个(10-20)图像(alpha' PNG' s)绘制到vb.net中的表单上而没有大量闪烁的方法。 主要问题是我在一个封闭的环境中 - 我无法安装任何SDK(因此没有DirectX)或redist(所以没有XNA) 我的第一个想法是GDI +,但我找不到一种不会闪烁的方式。一位朋友推荐使用SDL到C#包装器的SDL,虽然我在所有的c ++教程中都遇到了麻烦,而且如果我老实的话,我还想保留内置的winforms功能。 有人能提出更好的解决方案吗?
编辑:这是我的基本代码。每当它重新绘制时(即鼠标移动到表格背景上或表格背景上,例如表格上的另一个控件上)或当我使用注释掉的计时器控件(即使是1s刷新)而不是onpaint事件时,它会闪烁
Public Class Form1
Dim sprite As Image = Image.FromFile("C:\test\1.png")
Dim background As Image = Image.FromFile("C:\test\2.png")
Dim x As Integer = 20
Dim gfx As Graphics
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
gfx.DrawImage(background, 0, 0, background.Width, background.Height)
gfx.DrawImage(sprite, x + 50, x + 50, sprite.Width, sprite.Height)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
x = x + 10
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
gfx = Me.CreateGraphics
End Sub
'Private Sub smoothPainter_Tick(sender As Object, e As EventArgs) Handles smoothPainter.Tick
' gfx.DrawImage(background, 0, 0, background.Width, background.Height)
' gfx.DrawImage(sprite, x + 50, x + 50, sprite.Width, sprite.Height)
'End Sub
End Class
答案 0 :(得分:2)
不要创建静态图形对象。使用PaintEventArgs变量提供的图形对象。
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImage(background, 0, 0, background.Width, background.Height)
e.Graphics.DrawImage(sprite, x + 50, x + 50, sprite.Width, sprite.Height)
End Sub
还在表单上启用双缓冲
Private Sub Form1_Load(sender as Object, e as EventArgs) Handles Me.Load
Me.DoubleBuffered = True
End Sub
按钮单击
后,您可能还需要刷新表单Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
x = x + 10
Me.Invalidate() 'Or Me.Refresh if you want to force an instant redraw
End Sub
首先在图像上绘制然后将图像绘制到图像上通常会更快/更好(基本上就是双缓冲区所做的那样)。
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Using Bmp as New Bitmap(Me.Width, Me.Height)
Using g as Graphics = Graphics.FromImage(Bmp)
g.DrawImage(background, 0, 0, background.Width, background.Height)
g.DrawImage(sprite, x + 50, x + 50, sprite.Width, sprite.Height)
End Using
e.Graphics.DrawImageUnscaled(b, 0, 0)
End Using
End Sub