I've written some code so that I can select images from the server to display on my web site. It works fine in folders with around 100 images but in one folder which has 400 images I get an Out Of Memory Error. It only reads 144 images then gives the error. Can someone help me find out how I can fix this? Should I be disposing of something in this loop? Or is something else causing the error?
-------------------------
Dim xdt2 As DataTable = myAdsFromXmlFile.Tables(0)
Dim foundRows() As DataRow
Dim dr As DataRow
Dim myCounter As Integer = 0
Dim myCurrentlyUsedImages As Integer = 0
Dim lsErrorLocation As String = ""
Try
lsErrorLocation = "#3#"
Label1.Text = ParentDirectory.GetFiles("*.jpg").Length.ToString
For Each myfile In ParentDirectory.GetFiles("*.jpg")
myCounter = myCounter + 1
dr = dt.NewRow()
fullpath = "/photos/" & Session("sesPhotoCategory") & "/" & Session("sesFolder") & "/" & myfile.Name
dr("image") = "/photos/" & Session("sesPhotoCategory") & "/" & Session("sesFolder") & "/" & myfile.Name
dr("fname") = myfile.Name
Dim currentImage As System.Drawing.Image = System.Drawing.Image.FromFile(myfile.FullName)
Dim imgWidth As Integer = currentImage.Width
Dim imgHeight As Integer = currentImage.Height
currentImage.Dispose()
dr("imgw") = imgWidth
dr("imgh") = imgHeight
dr("fsize") = myfile.Length / 1024
dr("fpath") = fullpath
foundRows = xdt2.Select("ImageUrl='" + fullpath + "'")
dr("chkd") = (foundRows.Length > 0)
If foundRows.Length > 0 Then
myCurrentlyUsedImages = myCurrentlyUsedImages + 1
End If
dr("loop") = myCounter
foundRows = xdt2.Select("ImageUrl='" + fullpath + "' AND Keyword='new'")
dr("keywordnew") = (foundRows.Length > 0)
dt.Rows.Add(dr)
Next
Catch Ex As Exception
Label2.Text = "Error occurred: " & Session("sesPhotoCategory") & " page, loop " & myCounter & ", " & Ex.Message & " // " & vbNewLine & lsErrorLocation
End Try
-------------------------
Dim xdt2 As DataTable = myAdsFromXmlFile.Tables(0)
Dim foundRows() As DataRow
Dim dr As DataRow
Dim myCounter As Integer = 0
Dim myCurrentlyUsedImages As Integer = 0
Dim lsErrorLocation As String = ""
Try
lsErrorLocation = "#3#"
Label1.Text = ParentDirectory.GetFiles("*.jpg").Length.ToString
For Each myfile In ParentDirectory.GetFiles("*.jpg")
myCounter = myCounter + 1
dr = dt.NewRow()
fullpath = "/photos/" & Session("sesPhotoCategory") & "/" & Session("sesFolder") & "/" & myfile.Name
dr("image") = "/photos/" & Session("sesPhotoCategory") & "/" & Session("sesFolder") & "/" & myfile.Name
dr("fname") = myfile.Name
Dim currentImage As System.Drawing.Image = System.Drawing.Image.FromFile(myfile.FullName)
Dim imgWidth As Integer = currentImage.Width
Dim imgHeight As Integer = currentImage.Height
currentImage.Dispose()
dr("imgw") = imgWidth
dr("imgh") = imgHeight
dr("fsize") = myfile.Length / 1024
dr("fpath") = fullpath
foundRows = xdt2.Select("ImageUrl='" + fullpath + "'")
dr("chkd") = (foundRows.Length > 0)
If foundRows.Length > 0 Then
myCurrentlyUsedImages = myCurrentlyUsedImages + 1
End If
dr("loop") = myCounter
foundRows = xdt2.Select("ImageUrl='" + fullpath + "' AND Keyword='new'")
dr("keywordnew") = (foundRows.Length > 0)
dt.Rows.Add(dr)
Next
Catch Ex As Exception
Label2.Text = "Error occurred: " & Session("sesPhotoCategory") & " page, loop " & myCounter & ", " & Ex.Message & " // " & vbNewLine & lsErrorLocation
End Try