首先,我是Visual Basic.NET的新手,我在这里遇到了问题。 我开始编写一个screen2gif刻录机。在主要目的它是有效的。但是,如果我记录超过15到20秒,我的RAM会超载并超过x32bit应用程序的3.5 Gb限制。问题是位图我一遍又一遍地创建堆栈。
Private Function getBitmap(ByVal pCtrl As Control) As Bitmap
Dim myBmp As Bitmap
If myBmp IsNot Nothing Then
myBmp.Dispose()
End If
myBmp = New Bitmap(pCtrl.Width, pCtrl.Height)
Dim g As Graphics = Graphics.FromImage(myBmp)
Dim p As New Point(pCtrl.Parent.Width - pCtrl.Parent.ClientRectangle.Width - 4, pCtrl.Parent.Height - pCtrl.Parent.ClientRectangle.Height - 4)
g.CopyFromScreen(pCtrl.Parent.Location + pCtrl.Location + p, Point.Empty, myBmp.Size)
Dim LocalMousePosition As Point
LocalMousePosition = panelTranspacrency.PointToClient(Cursor.Position)
Cursor.Draw(g, New Rectangle(New Point(LocalMousePosition.X, LocalMousePosition.Y), Cursor.Size))
Return myBmp
myBmp.Dispose()
g.Dispose()
End Function
Private Sub tmrWork_Tick(sender As Object, e As EventArgs) Handles tmrWork.Tick
counter += 1
Dim bm As Bitmap
bm = getBitmap(Me.panelTranspacrency)
bm.Save(My.Settings.outputpath & "\temp\" & counter & ".png", Drawing.Imaging.ImageFormat.Png)
bm.Dispose()
End Sub
所以这是我创建位图并将其保存到磁盘的代码。 我提到我使用.Dispose命令但是ram不会自由。
请看一下并给我一个提示。提前谢谢。
答案 0 :(得分:0)
更改
End Using
到
Return myBmp
并将Using bm As Bitmap = getBitmap(Me.panelTranspacrency)
放在""
之后。你应该用
''
并删除所有明确的.Dispose调用。
<强>文档强> https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/using-statement