Out of memory

NeilA

Member
Joined
Aug 31, 2006
Messages
14
Programming Experience
1-3
My application loads an image from disk into a picturebox control on the form. The image is standard webcam size of 320x240 pixels and file size is about 5-15KB. The user clicks a button to load the image from the C drive where this application is and receives a Out of Memory error and no image loads.

It does however work on my PC where the application was developed. I've tried on another PC a laptop with 1.6gig cpu and 512 mb ram and it wont work on there either. My PC is 2.0gig cpu with 1gb ram. When running the app says it is using about 26mb ram in the processes list.

My code to load the image is:

VB.NET:
pbxPhoto.Image = Image.FromFile("C:\BusPassPhotos\" & file.Name)

file.Name contains the filename eg, "Picture 2.jpg"

Any ideas why I am getting this out of memory error?
 
An update:

This now does not work on my PC either. I was showing a colleague and typical it failed on my pc too!

I have now thought why this might be happening but not sure how to fix it. When my app loads it loads a jpg file similar to the web cam images as a default starting image. Then the user will click load to load in the newest image. The defaul image loads fine with no errors but anytime you try and load the newest cam image it errors. Is there something I need to do before loading the image like unbinding the other image or something?
 
Last edited:
Here is the full code I use:

VB.NET:
        Dim dir As New IO.DirectoryInfo(localphotos)
        Dim filelist As IO.FileInfo() = dir.GetFiles()
        Dim file As IO.FileInfo
        Dim count As Integer = 0
        Dim strFileName As String
        Dim filetochange As String
        Dim returnedname As String
        Dim found As Boolean = False

        For Each file In filelist

            If Microsoft.VisualBasic.LCase(Microsoft.VisualBasic.Left(file.Name, 7)) = "picture" Then

                count = count + 1
                found = True

            End If

        Next

        If found = True Then
            pbxPhoto.Image = Image.FromFile("C:\BusPassPhotos\" & file.Name)
            btnDelete.Enabled = True
            btnLoad.Enabled = False
        ElseIf found = False Then
            MessageBox.Show("There is no photo to preview", "Preview", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
 
The problem is here:
VB.NET:
        For Each file In filelist

            If Microsoft.VisualBasic.LCase(Microsoft.VisualBasic.Left(file.Name, 7)) = "picture" Then

                count = count + 1
                found = True

            End If

        Next
When you leave the for loop then your file.name is pointing to something other than an image file. In my case it was "thumbs.db".
Next is my solution. Also get away from vb6 conversion code. Red is what to add. Try this:
VB.NET:
		For Each file In filelist
			'If Microsoft.VisualBasic.LCase(Microsoft.VisualBasic.Left(file.Name, 7)) = "picture" Then
			If [COLOR="Red"]LCase(file.Name.ToString.Substring(0, 7)) [/COLOR]= "picture" Then
				count = count + 1
				found = True
				[COLOR="Red"]Exit For[/COLOR]
			End If

		Next
 
Hi

Thanks so much for this it work perfect now. I was doing my own bit of investigation and saw the thumbs.db file come up and then realised why it would not load the file. Thanks for the code tho this has helped me get around the problem.

Neil.
 
If you want to get completely away from VB6 convention you would use:
VB.NET:
file.Name.Substring(0, 7).ToLower
Notice there's no need to use the ToString function on the Name property of a FileInfo class as it is already a String.
 
Back
Top