Hi, I am using pictureboxes as buttons in VB .net. The main reason is to have a background image and when clicked on it changes pictures and when the mouse button is released the image is changed back to default image. My problem is each time the button is clicked it uses more and more memory after a couple of clicks - i do use "cleanup" code for the objects used. The code for loading my images is in an class.
Friend Sub LoadImageFromRes(ByVal ImageResName As String, ByVal DestCanvas As Object)
Dim tmpResBitmap As Bitmap Temp Bitmap from resourced
Dim tmpBitmap As New Bitmap(CInt(DestCanvas.Width), CInt(DestCanvas.Height)) Temp bitmap to draw to for stretching
Dim tmpGraphics As Graphics = Graphics.FromImage(tmpBitmap) Graphics obj used to stretch the resource bitmap
Resource to load the bitmap from
Dim ResManager As New Resources.ResourceManager("NewProfile.Data", System.Reflection.Assembly.GetExecutingAssembly)
tmpResBitmap = ResManager.GetObject(ImageResName) Get the temp bitmap from the resource to paint with
tmpGraphics.DrawImage(tmpResBitmap, 0, 0, CInt(DestCanvas.Width), CInt(DestCanvas.Height)) Stretch the resource bitmap to the temp bitmap
DestCanvas.image = tmpBitmap Draw the temp bitmap to destination
Objects Cleanup
tmpGraphics.Dispose()
tmpResBitmap = Nothing
tmpBitmap = Nothing
ResManager = Nothing
DestCanvas = Nothing
End Sub
The code is called from mouse down and up events. The bitmap is loaded from the resource. The destcanvas argument is a picturebox.
A object of the class is created then this sub is called and the the instance of this class is cleared.
Friend Sub LoadImageFromRes(ByVal ImageResName As String, ByVal DestCanvas As Object)
Dim tmpResBitmap As Bitmap Temp Bitmap from resourced
Dim tmpBitmap As New Bitmap(CInt(DestCanvas.Width), CInt(DestCanvas.Height)) Temp bitmap to draw to for stretching
Dim tmpGraphics As Graphics = Graphics.FromImage(tmpBitmap) Graphics obj used to stretch the resource bitmap
Resource to load the bitmap from
Dim ResManager As New Resources.ResourceManager("NewProfile.Data", System.Reflection.Assembly.GetExecutingAssembly)
tmpResBitmap = ResManager.GetObject(ImageResName) Get the temp bitmap from the resource to paint with
tmpGraphics.DrawImage(tmpResBitmap, 0, 0, CInt(DestCanvas.Width), CInt(DestCanvas.Height)) Stretch the resource bitmap to the temp bitmap
DestCanvas.image = tmpBitmap Draw the temp bitmap to destination
Objects Cleanup
tmpGraphics.Dispose()
tmpResBitmap = Nothing
tmpBitmap = Nothing
ResManager = Nothing
DestCanvas = Nothing
End Sub
The code is called from mouse down and up events. The bitmap is loaded from the resource. The destcanvas argument is a picturebox.
A object of the class is created then this sub is called and the the instance of this class is cleared.