Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Removing Images from Memory?

I fairly new to programming and am creating a screen saver that randomly displays images from a specified folder containing over 300 images. However, after each image has been displayed, they remain in memory. After several hours of the screen saver being active, my computer runs out of memory and tries to boost the virtual memory. I am not understanding why each image remains in memory. Here is a piece of my code:

'stores images to display in the background
Private backgroundPicture As Image

Private Sub backgroundChangeTimerTick(ByVal sender As Object, ByVal e As EventArgs) Handles backgroundChangeTimer.Tick
' Change the background image to the next image.
Randomize()
rand = Rnd() * 343 + 1
address = "E:\My Folders\Sean\My Pictures\Screen Saver\" & "screensaver (" & rand & ").JPG"
backgroundPicture = Image.FromFile(address)

Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
' Draw the current background image stretched to fill the full screen
e.Graphics.DrawImage(backgroundPicture, 0, 0, Size.Width, Size.Height)
End Sub

I thought that the image would be overwritten in memory since it is being stored as backgroundPicture. What am I doing wrong?
[1327 byte] By [fender5bass] at [2007-11-11 10:11:11]
# 1 Re: Removing Images from Memory?
Try adding backgroundPicture.Dispose() after the e.Graphics.DrawImage in OnPaintBackground.
Phil Weber at 2007-11-11 20:48:22 >