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()
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()