RudiF Posted January 4, 2006 Posted January 4, 2006 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. Quote
Administrators PlausiblyDamp Posted January 4, 2006 Administrators Posted January 4, 2006 Try changing the end of the routine to 'Objects Cleanup tmpGraphics.Dispose() tmpResBitmap.Dispose() tmpBitmap.Dispose() Calling dispose should free up the non-managed memory etc. used by the bitmap objects, setting them to nothing won't have any impact on the memory being freed so there is no need to include those lines. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.