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.

Or at least help me find a way to load the pictures without loading the memory(RAM) as it does now.

Thanks

Member Avatar for Unhnd_Exception
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ImageFromFile As Bitmap

        'This loads 16 picture boxes.  The image its loading is 2400 x 3000 jpg. 
        '2.5 megs.
        'This is really slow but it doesn't burn up much memory.

        For Each Control As Control In Me.Controls
            If Not TypeOf Control Is PictureBox Then Continue For

            'Get the image from file
            ImageFromFile = Image.FromFile(My.Computer.FileSystem.SpecialDirectories.MyPictures & "\Duck Hunt.jpg")

            'Set the picture box's image to a smaller version
            CType(Control, PictureBox).Image = New Bitmap(ImageFromFile, 50, 50)
            Control.Refresh() 'Immediatly show the image

            'Release memory from the main file image
            ImageFromFile.Dispose()
            ImageFromFile = Nothing
        Next

    End Sub

Thanks for your reply. I'll keep you in touch :)

Thanks m8. I did it thanks to you and it burns up just 30 Mb with my pictures (20 of them).

Here is the code:

Private Sub PanelPictures(ByVal picsoriz As Integer)

        '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
        Dim ImageFromFile As Bitmap

        'Panel is cleard by pictures and if error occurs, user is notified
        '   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()
                ImageFromFile = Image.FromFile(myPicFolder & getsexstr & "\" & Filename)

                'Gets number of files in folder
                Dim ints As Integer = IO.Directory.GetFiles(myPicFolder & getsexstr).Length

                'Picture settings; If there are more than 20 pictures in folder, then program shall load only 20
                If halt < 20 Then

                    With picBox
                        .Image = New Bitmap(ImageFromFile, 150, 200)
                        .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
                ImageFromFile.Dispose()
                ImageFromFile = Nothing
                'picBox.Dispose()

            Next

        Next

        '  Catch ex As Exception
        ' MsgBox(ex.Message)
        '    End Try

        halt = 0


    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.