vb.net - VB.NET 在函数调用中修改位图
问题描述
我正在尝试在函数调用中修改位图。没有函数调用我可以做
'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
这里我有一个内存泄漏,经过越来越多的迭代,它会崩溃。
我想在函数调用中,您正在返回一个位图以及位图通过 ByRef 传递的事实,因此它们将继续存在。我认为 BM.Dispose 可能会摆脱它 - 但事实并非如此。我不太确定如何解决我的内存泄漏(如果实际上是由于我的假设)。当然,我可以继续使用子例程,但我想知道如何以任何方式解决这个问题。任何帮助,将不胜感激。
解决方案
推荐阅读
- alexa - Alexa API 获取为已安装技能授予的所有权限
- html - 为什么 CSS width: calc(inherit) 做的事情与 width:inherit 不同?
- java - 这个for循环有什么问题?奇怪的错误
- python - 了解 NaN 的处理
- python - python .exe 文件不会打开/运行
- java - 使用spring boot,JPA通过引用关联实体
- networking - 寻找一种方法来确定为什么离开我的集群的数据包会保留它的 pod IP
- sqlite - Python3 | SQL 请求失败
- java - 我如何在 VSCode 上安装 junit 5
- ios - 如何在 numberofitemsinsection 中管理两个不同的单元格