核心代码如下:
Function GetSerPic(Optional ByVal BitWidth As Integer = -1, Optional ByVal BitHeight As Integer = -1) As Image
Dim SourceDC As IntPtr = Graphics.FromHwnd(Form1.DIVPanel.Handle).GetHdc()
Dim Bhandle, DestDC As IntPtr
DestDC = CreateCompatibleDC(SourceDC)
Bhandle = CreateCompatibleBitmap(SourceDC, BitWidth, BitHeight)
SelectObject(DestDC, Bhandle)
BitBlt(DestDC, 0, 0, BitWidth, BitHeight, SourceDC, 0, 0, &HCC0020)
Return Image.FromHbitmap(Bhandle)
End Function
,,我是把这段代码放在定时器里面运行的,差不多每秒钟增加5MB的内存,5分钟左右程序崩溃. 该怎么处理呢
VB.net 利用GDI截图内存不断增加的问题
答案:2 悬赏:0 手机版
解决时间 2021-02-07 05:26
- 提问者网友:沦陷
- 2021-02-06 10:53
最佳答案
- 五星知识达人网友:封刀令
- 2021-02-06 11:16
你光create位图,不释放,肯定崩溃呀。的释放,DeleteObject释放Bitmap,DeleteDC释放DC,另外Graphics.FromHwnd(Form1.DIVPanel.Handle)返回的DC也需要释放,代码改成这样:
Function GetSerPic(Optional ByVal BitWidth As Integer = -1, Optional ByVal BitHeight As Integer = -1) As Image
Dim g As Graphics = Graphics.FromHwnd(Me.DIVPanel.Handle)
Dim SourceDC As Long = g.GetHdc()
Dim Bhandle, DestDC As Long, Old As Long
DestDC = CreateCompatibleDC(SourceDC)
Bhandle = CreateCompatibleBitmap(SourceDC, BitWidth, BitHeight)
Old = SelectObject(DestDC, Bhandle)
BitBlt(DestDC, 0, 0, BitWidth, BitHeight, SourceDC, 0, 0, &HCC0020)
Dim bmp As Bitmap = Image.FromHbitmap(Bhandle)
SelectObject(DestDC, Old)
DeleteObject(Bhandle)
DeleteDC(DestDC)
g.ReleaseHdc()
Return bmp
End Function
Function GetSerPic(Optional ByVal BitWidth As Integer = -1, Optional ByVal BitHeight As Integer = -1) As Image
Dim g As Graphics = Graphics.FromHwnd(Me.DIVPanel.Handle)
Dim SourceDC As Long = g.GetHdc()
Dim Bhandle, DestDC As Long, Old As Long
DestDC = CreateCompatibleDC(SourceDC)
Bhandle = CreateCompatibleBitmap(SourceDC, BitWidth, BitHeight)
Old = SelectObject(DestDC, Bhandle)
BitBlt(DestDC, 0, 0, BitWidth, BitHeight, SourceDC, 0, 0, &HCC0020)
Dim bmp As Bitmap = Image.FromHbitmap(Bhandle)
SelectObject(DestDC, Old)
DeleteObject(Bhandle)
DeleteDC(DestDC)
g.ReleaseHdc()
Return bmp
End Function
全部回答
- 1楼网友:三千妖杀
- 2021-02-06 11:37
DestDC = CreateCompatibleDC(SourceDC)
Bhandle = CreateCompatibleBitmap(SourceDC, BitWidth, BitHeight)
都是建立内存,你最后要是放啊
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯