首页 > 解决方案 > 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 可能会摆脱它 - 但事实并非如此。我不太确定如何解决我的内存泄漏(如果实际上是由于我的假设)。当然,我可以继续使用子例程,但我想知道如何以任何方式解决这个问题。任何帮助,将不胜感激。

标签: vb.netfunctionbitmap

解决方案


推荐阅读