VB.NET Service + Out of Memory

R

Rocky

I have a vb.net service which copies images and resizes images from a local
directory to a remote one.

When I drop images into the local directory I get an out of memory error
after several images - the exception is on the following line.

Dim img As Image = Image.FromFile(path)

The machine has a GB of ram, and there is not a great deal running.
Each image is about 2000 x 2000 PIXELS

Thanks for any help in advance
 
T

Tom Shelton

Rocky said:
I have a vb.net service which copies images and resizes images from a local
directory to a remote one.

When I drop images into the local directory I get an out of memory error
after several images - the exception is on the following line.

Dim img As Image = Image.FromFile(path)

The machine has a GB of ram, and there is not a great deal running.
Each image is about 2000 x 2000 PIXELS

Thanks for any help in advance

Hmm... My guess would be that you aren't calling Dispose on the image
object. Just setting it to nothing will not free the native memory
used by the image. Of course, with out seeing the actuall code for
this routine, I am only guessing :)
 
R

Rocky

Hi Tom,

Here is the copy of the code

Dim asr As AppSettingsReader = New AppSettingsReader
Dim sRemoteDirectory As String = CType(asr.GetValue("RemoteDirectory",
GetType(String)), String)

Dim sBackupDirectory As String = CType(asr.GetValue("BackupDirectory",
GetType(String)), String)

Dim img As Image = Image.FromFile(path)

Dim imgFormat As Imaging.ImageFormat = GetImageFormat(path)

'Maintain aspect ratio

Dim scale As Double = 0

Dim width, height As Integer

width = CType(asr.GetValue("ImageWidth", GetType(String)), Integer)

height = CType(asr.GetValue("ImageHeight", GetType(String)), Integer)

'Caters for the user specifying 0 for either width or height

If width = 0 Then

width = 1

ElseIf height = 0 Then

height = 1

End If

If img.Height < img.Width Then

scale = width / img.Width

Else

scale = height / img.Height

End If

Dim newwidth As Integer = CInt(scale * img.Width)

Dim newheight As Integer = CInt(scale * img.Height)

Dim bmp As New Bitmap(img, newwidth, newheight)

img.Dispose()

'Add a log entry

SaveToLog(filename)

Dim sRemotePath As String = String.Format("{0}\{1}", sRemoteDirectory,
filename)

bmp.Save(sRemotePath, imgFormat)

bmp.Dispose()

Dim sBackupPath As String = String.Format("{0}\{1}", sBackupDirectory,
filename)

File.Copy(path, sBackupPath, True)

File.Delete(path)
 

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

Top