我正在尝试在函数调用中修改位图。 没有函数调用,我可以做
'MyBitMap - an existing bitmap of what I want to modify
Using NewBitMap as BitMap = MyBitMap.Clone 'make copy
Dim G as Graphics = Graphics.FromImage(MyBitMap) 'Draw graphics to MyBitMap
G.Clear(color.white) 'Clear image
G.DrawImage(NewBitMap, -10, 0) 'Shift Image to left 10
PictureBox1.Image = MyBitMap
End Using
工作正常,没有内存溢出或其他任何问题 在一个子项目中效果很好
Sub BMScroll_S(BM as BitMap, dx as integer, dy as integer)
using BMtemp as BitMap = BM.Clone
Dim G as Graphics = Graphics.FromImage(BM)
G.Clear(color.white)
G.DrawImage(BMTemp, dx, dy)
End Using
End Sub
Call BMScroll_S(MyBitMap, -10, 0)
PictureBox1.Image = MyBitMap
工作正常,但是如果我尝试创建一个函数以返回位图
Function BMScroll_F(BM as BitMap, dx as integer, dy as integer) as Bitmap
BMScroll_F = New Bitmap(BM)
Using BMtemp As Bitmap = BM.Clone
Dim G As Graphics = Graphics.FromImage(BMScroll_F)
G.Clear(Color.White)
G.DrawImage(BMtemp, dx, dy)
BM.Dispose()
End Using
End Function
MyBitMap=BMScroll_F(MyBitMap, -10, 0)
PictureBox1.Image = MyBitMap
这里有内存泄漏,并且经过越来越多的迭代,它将崩溃。
我想在函数调用中您将返回一个位图,并且事实是BitMap是通过ByRef传递的,因此它们将继续存在。我认为BM.Dispose可能会摆脱它-但事实并非如此。我不太确定如何解决我的内存泄漏(如果实际上是由于我的假设)。当然,我可以继续执行子例程,但是我想知道如何以任何方式解决此问题。任何帮助,将不胜感激。