Out of Memory Exception: System.Drawing.Image.FromFile

A

anastasia

I get an out of memory exception when attempting to excecute the
following code:

original = System.Drawing.Image.FromFile(file.FileName,true);

I ONLY get this exception when the file is in the "My Documents"
folder or subfolders. If the file lives anywhere else on the hard
drive, I have no problems.

What could be going on here?

Thanks in advance,
Stacey
 
P

PJ

I had this problem when first learning how to create thumbnails from images.
Unfortunately, I don't remember the specific problem....however, I think
you'd do much better off by creating an Image object from a byte array.
Grab the file from a FileStream object first and then create the image
object from a BinaryReader off the FileStream instance....

If this isn't helping you...post back and I will give you code I use to
manipulate images from files/streams....just don't have it now...it at work
;-).
 
V

Vincent V

Public Function resiseImage(ByVal fullPath As String, _

ByVal FinalSize As Integer, ByVal NewFullPath As String, _

Optional ByVal DeleteOriginal As Boolean = False) As String

Dim result As String = "Success"

Dim imagefolder As String = Path.GetPathRoot(fullPath)

Dim imagename As String = Path.GetFileName(fullPath)

Dim extension As String = Path.GetExtension(imagename)

Dim height As Integer

Dim width As Integer

Dim BiggerSide As String

Dim myratio As Decimal

Dim futureheight As Integer

Dim futurewidth As Integer

Dim Normal As System.Drawing.Image

Dim newimage As System.Drawing.Image

Normal = Normal.FromFile(fullPath)

height = Normal.Height

width = Normal.Width

If height > width Then

BiggerSide = "height"

myratio = FinalSize / width

futurewidth = myratio * width

Try

newimage = Normal.GetThumbnailImage(futurewidth, FinalSize, Nothing, New
IntPtr())

If File.Exists(imagefolder & imagename) Then File.Delete(imagefolder &
imagename)

newimage.Save(NewFullPath, System.Drawing.Imaging.ImageFormat.Jpeg)

Catch exp As Exception

result = "failed"

newimage.Dispose()

Throw exp

End Try



ElseIf width > height Then

BiggerSide = "width"

myratio = FinalSize / width

futureheight = myratio * height

Try

newimage = Normal.GetThumbnailImage(FinalSize, futureheight, Nothing, New
IntPtr())

If File.Exists(NewFullPath) Then File.Delete(imagefolder & imagename)

newimage.Save(NewFullPath, System.Drawing.Imaging.ImageFormat.Jpeg)

Catch exp As Exception

result = "failed"

newimage.Dispose()

Throw exp

End Try

Else

BiggerSide = "Same"

myratio = 1

Try

newimage = Normal.GetThumbnailImage(FinalSize, FinalSize, Nothing, New
IntPtr())

If File.Exists(NewFullPath) Then File.Delete(imagefolder & imagename)

newimage.Save(NewFullPath, System.Drawing.Imaging.ImageFormat.Jpeg)

Catch exp As Exception

newimage.Dispose()

result = "failed"

Throw exp

End Try

End If

Normal.Dispose()

newimage.Dispose()

If DeleteOriginal = True Then File.Delete(fullPath)

Return result

End Function

End Class
 
Joined
Apr 19, 2007
Messages
1
Reaction score
0
I am getting similar kind of error

Hi
I am getting the similar error.
I have a 6 MB JPG file. This file I receive in the form of byteArray from client. I create a stream object out of this byte array and load the image from this stream. Here is the code

Dim strm As New MemoryStream(data, False) 'data is the bytearray received from the client

dim img as image

img=img.fromstream(strm) ' This is the point where I get out of memory.

I have disposed off the image and everything the moment I create thumbnail. How do I resolve this?
 
Joined
Apr 18, 2008
Messages
1
Reaction score
0
From reading the above posts I was able to quickly figure out what my issue was. Maybe this is the same for you guys.

The Out of memory exceptions is being thrown because of the security setting on the file. I copied it into the IIS folders without resetting the permissions. Once I gave ASP.NET permissions on the file (or allowed it to inherit permissions from the parent folder) .... it loaded with no problem.

Hope this helps someone. And it only took me a year to respond. :)
 

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

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top