|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
OutOfMemory ExceptionCan 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() 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() > 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?!?!?! 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. -- Show quoteHide quoteEliyahu Goldin, Software Developer & Consultant Microsoft MVP [ASP.NET] "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() >
Other interesting topics
SQL Server does not exist...
WAP, VS2005 keeps changing my user control types!! CSS examples Howto inherit from an exisiting webcontrol in VS 2005 Page hang after updating web.config Help, NullReferenceException Force entire site HTTPS using Web.Config ASP.net using old version of DLLs Making New web site by using ASP.Net 2005 Multiple Page Tiff |
|||||||||||||||||||||||