利用BitBlt画的图,如何重画?

neighbornet 2012-01-11 01:07:40
用BitBlt在PictureBox上画图成功,但如果把PictureBox前面挡住的窗口移走后,PictureBox被挡住部分没有更新,请问应该如何刷新呢?用OnPaint事件也不行。


Public Class Form1

Declare Auto Function BitBlt Lib "GDI32.DLL" ( _
ByVal hdcDest As IntPtr, _
ByVal nXDest As Integer, _
ByVal nYDest As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hdcSrc As IntPtr, _
ByVal nXSrc As Integer, _
ByVal nYSrc As Integer, _
ByVal dwRop As Int32) As Boolean

Private Const ROP_SrcCopy As Integer = &HCC0020


Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

TestBitBlt()
End Sub

Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

End Sub


Private Sub TestBitBlt()
Dim bmp As Bitmap = Image.FromFile("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg")

Dim g1 As Graphics = Graphics.FromImage(bmp)
Dim g2 As Graphics = Me.PictureBox1.CreateGraphics
Dim hdc1 As IntPtr = g1.GetHdc
Dim hdc2 As IntPtr = g2.GetHdc
SelectObject(hdc1, bmp.GetHbitmap()) '
BitBlt(hdc2, 0, 0, bmp.Width, bmp.Height, hdc1, 0, 0, ROP_SrcCopy)
g1.ReleaseHdc()
g2.ReleaseHdc()
g1.Dispose()
g2.Dispose()
End Sub


End Class
...全文
149 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
neighbornet 2012-01-13
  • 打赏
  • 举报
回复
画到Me.PictureBox1。
尝试把TestBitBlt函数放到PictureBox的OnPaint事件中,在窗口或PictureBox1的Resize或其它事件中用PictureBox1.Invalidate或Refresh都没办法实现重画。

[Quote=引用 7 楼 jiantheone 的回复:]
你画到哪里的? Me.PictureBox1?
[/Quote]
hhddzz 2012-01-13
  • 打赏
  • 举报
回复
OnPaint里面g2不要用CreateGraphics,用e.Graphics
后面也不能Dispose g2,Hdc要释放掉。
jiantheone 2012-01-13
  • 打赏
  • 举报
回复
直接用GDI好了
Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

Dim bmp As Bitmap = Image.FromFile("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg")

Dim g As Graphics = e.Graphics
g.DrawImage(bmp, 0, 0)
End Sub

然后其他地方用PictureBox.Invalidate()就可以清除了 也能达到你想要的效果
neighbornet 2012-01-12
  • 打赏
  • 举报
回复
试过了,Paint事件也不行的

[Quote=引用 1 楼 jiantheone 的回复:]
先用 PictureBox.Invalidate() 清除图片 然后再重画 重画代码可以写在Paint事件里
[/Quote]
jiantheone 2012-01-12
  • 打赏
  • 举报
回复
直接用GDI好了 GDI用完干嘛还要绕个圈子去用 BitBlt
jiantheone 2012-01-12
  • 打赏
  • 举报
回复
你画到哪里的? Me.PictureBox1?
neighbornet 2012-01-12
  • 打赏
  • 举报
回复
试过了,没用。难为死了!!!!

[Quote=引用 5 楼 jiantheone 的回复:]
PictureBox.Invalidate() 要写在别的地方 然后Paint事件里写重画代码
[/Quote]
jiantheone 2012-01-12
  • 打赏
  • 举报
回复
PictureBox.Invalidate() 要写在别的地方 然后Paint事件里写重画代码
devmiao 2012-01-12
  • 打赏
  • 举报
回复
不好意思可能还得画一次哦,没办法恢复的,喵~
jiantheone 2012-01-11
  • 打赏
  • 举报
回复
GDI绘图不错 图不是很大的话 没必要用API BitBlt
jiantheone 2012-01-11
  • 打赏
  • 举报
回复
先用 PictureBox.Invalidate() 清除图片 然后再重画 重画代码可以写在Paint事件里

16,554

社区成员

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

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