GDI+ generic error : memory leak using VB.NET

北城以北 提交于 2019-12-13 19:15:14

问题


I got a memory leak problem with my code, using images with VB.NET and WPF. Even if I dispose my bitmap objects, the memory doesn't stop growing.

Here is my code

Dim imageB As New ImageBrush                
imageB.ImageSource = Me.GetImageSource(My.Resources.checkImage)
My.Resources.checkImage.Dispose()
Me.mainCanvas.Background = imageB

and here is the method GetImageSource():

Public Function GetImageSource(ByVal bmp As System.Drawing.Bitmap) As ImageSource
    Dim imageSource As BitmapSource
    Dim hbitmap As IntPtr = bmp.GetHbitmap
    Dim sizeOptions As BitmapSizeOptions = BitmapSizeOptions.FromEmptyOptions()
    imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, sizeOptions)
    ImageSource.Freeze()
    DeleteObject(hbitmap)
    bmp.Dispose()
    Return imageSource
End Function

Any ideas ? Thanks.

来源:https://stackoverflow.com/questions/8836964/gdi-generic-error-memory-leak-using-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!