473,404 Members | 2,174 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

OutOfMemory Exception

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

Sep 6 '06 #1
3 2892
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]
"Nemisis" <da*********@hotmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.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()

Sep 6 '06 #2
Because your server is out of memory perhaps????

Nemisis wrote:
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()
Sep 6 '06 #3
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?!?!?!

Sep 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Schilling | last post by:
Instances of SystemOutOfMemoryException do not contain a stack trace. Easy test to verify this: class OOM { public static void Main() { try { Object arr = new Object; } catch...
1
by: Diablo | last post by:
Hi, I'm working on medium size project in C#. The project involves working with an Access database through the Jet engine. I have almost completed the project and now i am doing some testing....
0
by: vMike | last post by:
If a website is running on a shared server and a system.outofmemory exception is thrown, is the error because of my website application or could it be because of anyone's website application. If it...
2
by: Ricky Chan | last post by:
Windows Server 2003 with 2G Ram ,IIS6 -> I have enabled /3gb switch in boot.ini ! -> Machine.config set to 80% memory Limit However, It still throw outofmemory exception when loading large...
1
by: Ricky Chan | last post by:
In the production environment, it always occurs and the worker process did not recycle automatically. Therefore, it make the system service break to client. In development environment, we write...
0
by: Joe Ross | last post by:
(Apologies in advance if there is a better forum for asking advice on this topic). Our ASP.NET application occasionally starts spitting out OutOfMemory exceptions. When this happens, the memory...
5
by: LP | last post by:
Hello, We running VB.NET application which gets massive amounts of data from SQL Server, loads data into DataTables, then re-arranges data into tabular structure and outputs it to a flat file....
2
by: Peter S. | last post by:
I am pulling some data from a source via ODBC and placing the information in a DataSet. The first pull is very large but once that is complete I plan to do nightly pulls to get any new data that...
8
by: Mantorok | last post by:
Hi all I'm populating a DataTable with around 1,500,000 rows, the table contains 4 columns, 3 string columns and a decimal column. However, I keep getting OutOfMemory exceptions when my app...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.