OutOfMemory Exception

N

Nemisis

Hi everyone,

Can someone please tell me why my code hits an "out of memory
exception" on the below code?

All the code does is load some documents from a SQL database and loop
through a data reader to create a thumbnail image for each image in my
dataReader.

The error only occurs when i have more then 3 images in my DataReader.

' ** Get the images for the measure
Dim oConn As SqlConnection
Dim oComm As SqlCommand
Dim oDataR As SqlDataReader

Dim oData() As Byte
Dim bmp As Bitmap
Dim thumbnail As Bitmap
Dim tmpFileName As String
Dim myCallback As New
Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)

' ** Get the images for the measure
oConn = New SqlConnection(ConnectionString)
oComm = New SqlCommand("Select * From Documents Where
Name Like 'Bob%'", oConn)

oConn.Open()

oComm.Parameters.Add(New SqlParameter("@ForID",
oScore.ScoreID))

oDataR = oComm.ExecuteReader()

' loop thorough images that were uploaded against the
measure
While oDataR.Read

' get temporyary file name to save file
tmpFileName = Path.GetTempFileName

' delete file that is automatically created
File.Delete(tmpFileName)

' strip out file extension
tmpFileName =
Path.GetFileNameWithoutExtension(tmpFileName)

' set byte array equal to documents data
oData = oDataR.Item("DocumentData")

' write byte array to file and save it
File.WriteAllBytes(Current.Server.MapPath("images/"
& tmpFileName), oData)

' set bitmap as image
bmp = New Bitmap(Current.Server.MapPath("images/" &
tmpFileName))

' work out the ratio to reduce the image by
If (bmp.Height / 200) > (bmp.Width / 260) Then
imageSizeMultipler = bmp.Height / 200
Else
imageSizeMultipler = bmp.Width / 260
End If

' create thumbnail
thumbnail = bmp.GetThumbnailImage(bmp.Width /
imageSizeMultipler, bmp.Height / imageSizeMultipler, myCallback,
IntPtr.Zero)

' save thumbnail and destroy object
thumbnail.Save(Current.Server.MapPath("images/" &
tmpFileName & ".png"))
thumbnail.Dispose()

bmp.Dispose()

If File.Exists(Current.Server.MapPath("images/" &
tmpFileName)) Then
File.Delete(Current.Server.MapPath("images/" &
tmpFileName))
End If
End While

oDataR.Close()

oComm.Dispose()

oConn.Close()
oConn.Dispose()
 
E

Eliyahu Goldin

Just a hint. You are using GDI+. It is known for throwing an OutOfMemory
exception when something is wrong with image files. Most recently I had one
when I tried to open a multi-paged tiff file with pages in different format.
 
N

Nemisis

I have been reading many articles and most people seem to say that this
sint always an out of memory error? This true?

Does anyone know how i can fix this, IN CODE?!?!?!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top