Show photo of a listbox item gives me error Out Of Memory

1

I am making a tool that transforms a photo upload by the user into bytes array then compares those bytes with the photos in the database

I was able to make the code work but when in the end it detected something the same or similar then it shows the one used in picturebox1 and the one found in the database that in this case is inserted in a listbox when the program does load .

When it finishes the operation after finding a match it gives me error.

My error is:

  

Out Of Memory is in function timer1

My code is:

Public Class Form3
Dim totalbytes As Integer


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Using fo As New OpenFileDialog
        If fo.ShowDialog = DialogResult.OK Then
            Dim fs As New IO.FileStream(fo.FileName, IO.FileMode.Open)
            Dim br As New IO.BinaryReader(fs)
            Dim byteArea As Byte()
            byteArea = br.ReadBytes(CInt(fs.Length))
            totalbytes = fs.Length
            br.Close()
            'just to show the sample without a fileread error
            'be aware not to use a dispose or using here
            'it gives a GDI+ error as the stream (not the name) is reused.
            Dim ms As New IO.MemoryStream(byteArea)
            PictureBox1.Image = Image.FromStream(ms)
        End If
    End Using
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


    Dim byteArea As Byte()
    Using msX As New IO.MemoryStream
        PictureBox1.Image.Save(msX, Imaging.ImageFormat.Bmp)
        byteArea = msX.ToArray()
    End Using
    Using ms2 As New IO.MemoryStream(byteArea)
        Me.PictureBox2.Image = Image.FromStream(ms2)
    End Using
    Me.PictureBox1.Image = Nothing
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim finfo As New IO.DirectoryInfo("C:\flora")
    For Each fi In finfo.GetFiles
        ListBox1.Items.Add(fi.FullName)
    Next
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1


    ''PictureBox1.Image = Image.FromFile(Me.ListBox1.SelectedItem.ToString())
    Dim ffs As New IO.FileStream(ListBox1.SelectedItem, IO.FileMode.Open)
    Dim bbr As New IO.BinaryReader(ffs)
    Dim byteArea As Byte()
    byteArea = bbr.ReadBytes(CInt(ffs.Length))
    If ffs.Length = totalbytes Then
        Timer1.Stop()
        Dim pict2 As String = ListBox1.SelectedItem.ToString
        PictureBox2.Image = Image.FromFile(pict2)

        MsgBox("Resultados Encontrado " + ListBox1.SelectedItem)

    End If

    ''MessageBox.Show(fs.Length)
    bbr.Close()
    'just to show the sample without a fileread error
    'be aware not to use a dispose or using here
    'it gives a GDI+ error as the stream (not the name) is reused.
    Dim ms As New IO.MemoryStream(byteArea)
    PictureBox1.Image = Image.FromStream(ms)

    ''MessageBox.Show(ms.ReadByte)

    ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1
End Sub
End Class
    
asked by anonymous 26.12.2017 / 15:40

1 answer

3

The code is not releasing fs , nor ms , although the latter does not know if it could. There is no guarantee that br will be released like this. It has other variables that hold objects longer than necessary. And from what you can see in the code you may have other problems at other points.

If you know how to use Using , as shown in the code, make use of it to resolve these issues. If the problem persists you have problems elsewhere.

    
27.12.2017 / 01:08