picturebox的autoredraw问题

timezone 2011-02-17 07:06:02
在网上搜索了一下在.net中的picturebox可以用下面的代码做到,我想用多个线程来操作picturebox,发现如果不用timer中的PictureBox1.Image = bgimage 来不断刷新就实现不了。如何让picturebox能真正的实现自动刷新?而不用手动的来刷新?
Public Class Form1
Dim g2 As Graphics
Dim xp1 As Bitmap = My.Resources.sanjiao
Dim bgimage As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

bgimage = New Bitmap(PictureBox1.Width, PictureBox1.Height) '创建相同大小的内存位图
g2 = Graphics.FromImage(bgimage)
PictureBox1.Image = bgimage
g2.DrawImage(My.Resources.tr, 0, 0)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t1 As New Thread(AddressOf thread1)
t1.Start()
End Sub
Private Sub thread1()
For a As Integer = 1 To 10000000
g2.DrawImage(xp1, 20, 20)
Thread.Sleep(1)
Next
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
PictureBox1.Image = bgimage
End Sub

End Class
...全文
179 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dylike 2011-08-05
  • 打赏
  • 举报
回复
没有看懂.说说你想实现什么?
timezone 2011-02-18
  • 打赏
  • 举报
回复
还有没有高手指点一下啊
timezone 2011-02-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xingyuebuyu 的回复:]
VB.NET code
Public Class Form1

Dim g2 As Graphics
Dim xp1 As Bitmap = My.Resources.sanjiao
Dim bgimage As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e ……
[/Quote]
感谢楼上的高手的回答,如果我在线程中有许多drawimage和drawstring的操作,而且在线程里的图像操作是变化的,那就不可能全部放到onpain中吧。现在我用线程来绘图这些没有问题,问题是因为要用到timer来PictureBox1.Image = bgimage
不断刷新实现绘制的内容反应到Picturebox上而占用太高的cpu,请问有没有什么办法能实时的更新到picturebox上
xingyuebuyu 2011-02-17
  • 打赏
  • 举报
回复
Public Class Form1

Dim g2 As Graphics
Dim xp1 As Bitmap = My.Resources.sanjiao
Dim bgimage As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bgimage = New Bitmap(PictureBox1.Width, PictureBox1.Height) '创建相同大小的内存位图
g2 = Graphics.FromImage(bgimage)
PictureBox1.Image = bgimage

End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If g2 IsNot Nothing AndAlso xp1 IsNot Nothing Then
g2.DrawImage(My.Resources.tr, 0, 0)
g2.DrawImage(xp1, 20, 20)
End If
End Sub
End Class


绘图放在Paint事件中就会自动刷新的
timezone 2011-02-17
  • 打赏
  • 举报
回复
楼上的这位大侠回复的是什么意思啊?用invalidate吗?
wuyq11 2011-02-17
  • 打赏
  • 举报
回复
Invalidate作用是重新执行paint以更新失效区域
using (bitmap B = new bitmap(picturebox1.width, picturebox1.height)) {
using (graphics G = graphics.fromimage(B)) {
point P = new point(3, 3);
point q = new point(5, 5);
pen pen = new pen(color.blue, 3);
G.DrawLinde(pen, P, q);
}
picturebox1.Image = B;
}

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧