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

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

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

    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
    anastasia Guest

  2. Similar Questions and Discussions

    1. system.drawing ASP.NET VB Problem
      I'm having probelms trying to write text to an image coming from a database. I started this using the article on this site but the majority of all...
    2. System.Drawing and GDI+ Question - Please help
      I have a winforms control that draws a rectangle for a boarder and positions some graphical elements around the control to create the boarder effect...
    3. problem system.drawing transparent web images
      Here's the scenario: I've got a whole bunch (for the sake of argument, let's say thousands) of different little 32x14 .png files that act as...
    4. convert hex to System.Drawing.Color
      Hi, Here is the sample. int oleColor = 0xFF00; // Translate oleColor to a GDI+ Color structure. Color myColor =...
    5. SQL plus memory exception
      Great !! I sometimes had/have similar effects with DESCRIBE the real funny thing I noticed: the crash of sqlplus is platform independent !!...
  3. #2

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

    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
    ;-).

    "anastasia" <ana_stasie@yahoo.com> wrote in message
    news:fdf3218d.0307312318.ebe30f9@posting.google.co m...
    > 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

    PJ Guest

  4. #3

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

    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


    "PJ" <pjwalNOSPAM@hotmail.com> wrote in message
    news:u30h4kAWDHA.1748@TK2MSFTNGP12.phx.gbl...
    > 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
    > ;-).
    >
    > "anastasia" <ana_stasie@yahoo.com> wrote in message
    > news:fdf3218d.0307312318.ebe30f9@posting.google.co m...
    > > 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
    >
    >

    Vincent V Guest

  5. #4

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

    Quote Originally Posted by anastasia View Post
    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
    You are probably running Vista and being punished by its weirdness. Typically you won't get this error if the file is located in a 'normal' folder that doesn't have any special permissions. Big trouble from 'sensitive' directories like "C:\" or "C:\Windows\System32" or... "My Documents". This is from another guy's post on the subject:

    "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."

    Possible workarounds: 1) Don't access files in sensitive folders, 2) Copy file to a 'normal' folder and then access and then delete when finished, 3) Change permissions.
    Dean Hough Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139