Windows Service - Out of memory

R

Rocky

I have created a windows service using vb.net which uses a filesystemwatcher
to monitoe files in a specifc directory, if images are added to the
directory they are resized and saved in a remote directory. The original
file is then backed up and deleted from the image directory.

The code is as follows

Friend Shared Sub ResizeImage(ByVal path As String, ByVal filename As
String)

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 = ImageFromFile(path)

(This was put in as an alternative but produced the same problem

//

Dim fs As FileStream = New FileStream(filename, FileMode.Open,
FileAccess.Read)

Return Image.FromStream(fs)

fs.Dispose()

//

Dim img As Image = Image.FromFile(path) << It fails here

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)

Whether I copy a couple of images or lots, the error is always the same

The call stack is as follows

[External Code]
GeminiPictureService.exe!GeminiPictureService.GeneralRoutines.ResizeImage(string
path = "C:\\Picture Test 1\\IMGP0353.JPG", string filename =
"IMGP0353.JPG") Line 18 + 0x9 bytes Unknown
GeminiPictureService.exe!GeminiPictureService.GeminiPictureService.fswGemini_Created(object
sender = {System.IO.FileSystemWatcher}, System.IO.FileSystemEventArgs e =
{System.IO.FileSystemEventArgs}) Line 42 + 0x20 bytes Unknown
[External Code]


Exception

- $exception {System.OutOfMemoryException: Out of memory.
at System.Drawing.Image.FromFile(String filename, Boolean
useEmbeddedColorManagement)
at System.Drawing.Image.FromFile(String filename)
at GeminiPictureService.GeneralRoutines.ResizeImage(String path, String
filename)
at GeminiPictureService.GeminiPictureService.fswGemini_Created(Object
sender, FileSystemEventArgs e)
at System.IO.FileSystemWatcher.OnCreated(FileSystemEventArgs e)
at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action,
String name)
at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode,
UInt32 numBytes, NativeOverlapped* overlappedPointer)
at
System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32
errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)} System.Exception
{System.OutOfMemoryException}

Memory - I checked the process and the memory usage spiked from 8,000 to
(40-46k), it stayed at around 40k before giving an exception

System Pentium 1.8GHz 1GB Ram Windows XP Pro SP2

Thanks for any help in advance
 
M

Mehdi

Exception

- $exception {System.OutOfMemoryException: Out of memory.
at System.Drawing.Image.FromFile(String filename, Boolean
useEmbeddedColorManagement)
at System.Drawing.Image.FromFile(String filename)

GDI+ tends to throw OutOfMemoryExceptions whenever something goes wrong
event if that's got nothing to do with a memory problem. In the case of
Image.FromFile(), I *think* that you might get an OutOfMemoryException if
the image is corrupted or has an unsuported format. I would investigate
this first.
 

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