I have the following code. My program loads 20 pictures in one panel and each pictures has for the moment like 500 Kb, but in the future they shall have like 2~6 Mb.
When I use this part of code, my program that uses only 6 Mb of memory, but jumpes at 202 Mb when pics are loaded. I need a method to resize or make them thumbnail or something to reduce the memory.
I don't know for the moment how to use the System.Threading, but I want to know if it shall help my program, if not, then help me replace parts of my code.
Private Sub PanelPictures(ByVal picsoriz As Integer) ' picoriz represents how many pictures should the panel have on each row (it's set at 5 pictures per line/row)
'Tooltip created so that, when mouse hover over picture it displays picture name.
Dim tooltippic As New ToolTip
Dim picBox As PictureBox
Dim nextrow As Integer = 3
'Panel is cleared
Try
Panel1.Controls.Clear()
Dim di As New IO.DirectoryInfo(myPicFolder & getsexstr)
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
For i As Integer = 0 To picsoriz
Application.DoEvents()
'List the names of all files in the specified directory
For Each dra In diar1
Filename = dra.ToString
picBox = New PictureBox()
'Gets number of files in folder
Dim ints As Integer = IO.Directory.GetFiles(myPicFolder & getsexstr).Length
open = di.ToString
'Picture settings; If there are more than 20 pictures in folder, then program shall load only 20
If halt < 20 Then
With picBox
.Image = Image.FromFile(myPicFolder & getsexstr & "\" & Filename)
.Height = 200
.Width = 150
.Location = New Point(horz, nextrow) ' Client coordinates relative to the form
.SizeMode = PictureBoxSizeMode.StretchImage
.Tag = myPicFolder & getsexstr & "\" & Filename 'Tag so that the click event knows what to start (tag holds the path of file)
End With
Else
Exit For
End If
'Next picturebox is set 3 pixels apart
horz += 153
'Counter is incremented every time so that it can count the number of pictures in the panel
count = count + 1
If count = picsoriz Then 'Checks if the number of pictures in panel equals the number of pictures set by me
nextrow = nextrow + 203 'Next row of pictures is set at a distance of 203+ pixels
horz = 3
count = 0
i += 1
ElseIf ints < picsoriz Then 'If number of files in folder is less then use defined pictures in panel then variable count gets the difference between number of picters set by user and number of files in folder
count = picsoriz - ints
i += count + 1
Else
i += 1
End If
'Tooltip; When mouse hover over picture, filename is displayed
tooltippic.ShowAlways = True
tooltippic.UseFading = True
tooltippic.InitialDelay = 300
tooltippic.SetToolTip(picBox, Filename)
AddHandler picBox.Click, AddressOf picBox_CLick
'Picturebox is added to panel
Panel1.Controls.Add(picBox)
halt += 1'
Next
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
halt = 0
End Sub
Regards, Cosmin.