I am making an image slider/carousel type in Windows Form [VB.NET], I am using a Timer so that every so often the images are presented in a PictureBox. Until now the program does what I want, only that it causes me a big problem, every time an image is loaded in the PictureBox the memory consumption of the system increases until it reaches the point of crashing the program.
The code I use to do this task:
Dim lista As New List(Of String)
Dim i As Integer = 0
Get the name of the files (jpg) in a directory and store them in a list.
Sub llenardatos()
Dim carpeta As New DirectoryInfo("C:\Users\Usuario\Desktop\Promo\")
For Each File As FileInfo In carpeta.GetFiles("*.jpg")
lista.Add(File.Name.ToString)
Next
End Sub
When executing the program, load the first element of the list.
Private Sub frmP_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
llenardatos()
Timer1.Start()
Timer1.Interval = 3000
PictureBox1.Image = Image.FromFile("C:\Users\Usuario\Desktop\Promo\" + lista(0))
End Sub
Use of Timer, show in PictureBox the elements of the list, if they reach the end, restart the counter.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
i = i + 1
Label1.Text = i
If i = lista.Count Then
i = 0
End If
PictureBox1.Image = Image.FromFile("C:\Users\Usuario\Desktop\Promo\" + lista(i))
End Sub
I hope you can guide me, which is what causes the problem, it should be noted that the images are light since the heaviest img is 468KB
I think your problem might be because you don't make
Dispose
the images you don't use again.Under this premise, and based on this answer , you should Dispose of the images every time you update the
PictureBox
:VB.net is not my forte, so please forgive me if my syntax is wrong.