Home All Groups Group Topic Archive Search About
Author
6 Sep 2006 2:08 PM
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()

Author
6 Sep 2006 2:27 PM
Ray Booysen
Because your server is out of memory perhaps????

Nemisis wrote:
Show quoteHide quote
> 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()
>
Are all your drivers up to date? click for free checkup

Author
7 Sep 2006 8:09 AM
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?!?!?!
Author
6 Sep 2006 3:23 PM
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.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]


Show quoteHide quote
"Nemisis" <darrens2***@hotmail.com> wrote in message
news:1157551683.804150.108210@h48g2000cwc.googlegroups.com...
> 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()
>

Bookmark and Share