user1735880
user1735880

Reputation: 13

vb.net - Running out of memory when working with a lot of images

I'm trying to check a set of images (usually more than 50, every one around 3 Mb) for their orientation. I get "Out of memory" error when I have already processed a bunch of them.

So my question is how do I check each image separately in order to use the least memory ?

I'm really really new to vb.net and programming at all, so this was the only way I could figure out how to do my task :

  Dim MyFiles As New ArrayList()

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    TextBox2.Clear()

    If CheckBox2.Checked = True Then

        TextBox2.Text = "Checking the orientation of the images ..." & vbNewLine & vbNewLine

        For i As Integer = 0 To MyFiles.Count - 1

            TextBox2.AppendText("Checking " & MyFiles(i) & vbNewLine)
            If Image.FromFile(MyFiles(i)).Width < Image.FromFile(MyFiles(i)).Height Then
                TextBox2.AppendText(vbNewLine & "There are images with portrait orientation. Splitting aborted!" & vbNewLine)
                Return
            End If

        Next

        TextBox2.AppendText(vbNewLine & "All images are with landscape orientation." & vbNewLine)

    End If

    'ConvertBMP("C:\test.bmp", ImageFormat.Jpeg)

End Sub

Upvotes: 1

Views: 559

Answers (1)

Alan Jackson
Alan Jackson

Reputation: 6511

Throw a using() around the Image.FromFile command. Also, you should just build the image once and check the width/height once rather than decoding it twice.

In c# it would look like this:

using (var img = Image.FromFile(filename))
{
    if (img.Width < img.Height)
        doSomething();
}

or in VB.Net (my VB.Net is a little rusty, but I think this is correct):

Dim img as Image
Using img = Image.FromFile(filename)
    If img.Width < img.Height
        TextBox2.AppendText(vbNewLine & "There are images with portrait orientation. Splitting aborted!" & vbNewLine)
        Return
    End If
End Using

Upvotes: 1

Related Questions