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

Asked

Viewed 61 times

1

I’m making a tool that turns a photo made upload by the user in bytes array then compare these Bites with the existing photos in the database

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

When the operation ends after finding a match he gives me error.

My mistake is:

Out Of Memory in timer1 function

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

1 answer

3


The code is not releasing the fs, nor the ms, although the latter do not know if it could. There is no guarantee that the br will be released like this. It has other variables that hold objects more 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 the Using, as shown in the code, make use of it to solve these problems. If the problem persists you have problems elsewhere.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.