Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. "Out of memory exception when loading image with bitmap

"Out of memory exception when loading image with bitmap

Scheduled Pinned Locked Moved C#
graphicshelpmysqldesignsysadmin
10 Posts 4 Posters 20 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Casper Hansen
    wrote on last edited by
    #1

    Hello all I have a mysql table with some rows in it that contain a path to a jpg file. Here is a example of the path: images/portfolio/lille_ilveaWebshop.jpg The file excists on the webhost so that is not a problem. I am loading the jpg file into a bitmap in a foreach. It works fine when I am on a local server, but when I upload it to my webhost it says: <quote>Server Error in '/' Application. Out of memory. Exception Details: System.OutOfMemoryException: Out of memory. [OutOfMemoryException: Out of memory.] System.Drawing.Bitmap..ctor(String filename) +376480 Referencer.Page_Load(Object sender, EventArgs e) +561 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627</quote> I cant seem to figure out just why it would do that. Here is the code I am using: foreach ( DataRow Row in objPortFac.VisReference(refKatID).Rows) { // Create a new bitmap with the path from the row // fldStortBillede is the field that contains the path Bitmap objStortBillede = new Bitmap(Server.MapPath(Row["fldStortBillede"].ToString())); // Dispose the bitmap after use objStortBillede.Dispose(); } Any suggestions?

    L 1 Reply Last reply
    0
    • C Casper Hansen

      Hello all I have a mysql table with some rows in it that contain a path to a jpg file. Here is a example of the path: images/portfolio/lille_ilveaWebshop.jpg The file excists on the webhost so that is not a problem. I am loading the jpg file into a bitmap in a foreach. It works fine when I am on a local server, but when I upload it to my webhost it says: <quote>Server Error in '/' Application. Out of memory. Exception Details: System.OutOfMemoryException: Out of memory. [OutOfMemoryException: Out of memory.] System.Drawing.Bitmap..ctor(String filename) +376480 Referencer.Page_Load(Object sender, EventArgs e) +561 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +50 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627</quote> I cant seem to figure out just why it would do that. Here is the code I am using: foreach ( DataRow Row in objPortFac.VisReference(refKatID).Rows) { // Create a new bitmap with the path from the row // fldStortBillede is the field that contains the path Bitmap objStortBillede = new Bitmap(Server.MapPath(Row["fldStortBillede"].ToString())); // Dispose the bitmap after use objStortBillede.Dispose(); } Any suggestions?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, some image operators (e.g. Image.FromFile) throw an OOM Exception when the data is not valid. I don't know what Server.MapPath is assumed to do, nor how valid its result is. Split the statement in simpler ones and debug. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


      C M 2 Replies Last reply
      0
      • L Luc Pattyn

        Hi, some image operators (e.g. Image.FromFile) throw an OOM Exception when the data is not valid. I don't know what Server.MapPath is assumed to do, nor how valid its result is. Split the statement in simpler ones and debug. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


        C Offline
        C Offline
        Casper Hansen
        wrote on last edited by
        #3

        The "new bitmap" needs a full path an images/something/blah.jpg doesnt work. I am using Server.MapPath to find the full path

        L 1 Reply Last reply
        0
        • C Casper Hansen

          The "new bitmap" needs a full path an images/something/blah.jpg doesnt work. I am using Server.MapPath to find the full path

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Have you checked the value of Server.MapPath(Row["fldStortBillede"].ToString()? MSDN: "For security reasons, the AspEnableParentPaths property has a default value set to FALSE. Scripts will not have access to the physical directory structure unless AspEnableParentPaths is set to TRUE." :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


          C 1 Reply Last reply
          0
          • L Luc Pattyn

            Have you checked the value of Server.MapPath(Row["fldStortBillede"].ToString()? MSDN: "For security reasons, the AspEnableParentPaths property has a default value set to FALSE. Scripts will not have access to the physical directory structure unless AspEnableParentPaths is set to TRUE." :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


            C Offline
            C Offline
            Casper Hansen
            wrote on last edited by
            #5

            The value on localhost is: D:\Skole\DotNet\Portfolio\Portfolio\images\portfolio\stor_cronaldoHjemmeside.jpg And on the webhost it is: d:\web\localuser\dumpen.dk\public_html\images\portfolio\stor_cronaldoHjemmeside.jpg It works with 2 or 3 images but with more then that the error occours

            L 1 Reply Last reply
            0
            • C Casper Hansen

              The value on localhost is: D:\Skole\DotNet\Portfolio\Portfolio\images\portfolio\stor_cronaldoHjemmeside.jpg And on the webhost it is: d:\web\localuser\dumpen.dk\public_html\images\portfolio\stor_cronaldoHjemmeside.jpg It works with 2 or 3 images but with more then that the error occours

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              is it always the same image that fails, if so it got damaged. try changing the order just to see what happens. are these images large, compared to your system (is it a mobile Windows system with limited RAM?). :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


              R 1 Reply Last reply
              0
              • L Luc Pattyn

                is it always the same image that fails, if so it got damaged. try changing the order just to see what happens. are these images large, compared to your system (is it a mobile Windows system with limited RAM?). :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                R Offline
                R Offline
                Revathij
                wrote on last edited by
                #7

                I'm also having the same Problem. In my local it is working properly. But in server, it is throwing out of memory exception. i got this exception even though the memory is available in server. Graphics g = Graphics.FromImage(source); try { g.DrawImageUnscaled(original, 0, 0); } finally { ((IDisposable)g).Dispose(); } This is the code i'm using. Any solutions to this issue? Thanks in advance :)

                L 1 Reply Last reply
                0
                • R Revathij

                  I'm also having the same Problem. In my local it is working properly. But in server, it is throwing out of memory exception. i got this exception even though the memory is available in server. Graphics g = Graphics.FromImage(source); try { g.DrawImageUnscaled(original, 0, 0); } finally { ((IDisposable)g).Dispose(); } This is the code i'm using. Any solutions to this issue? Thanks in advance :)

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  insufficient information. IMO DrawImageUnscaled is more critical than DrawImage, as the resolution may be screwed inside the image (both source and original). Try DrawImage instead. And log all image parameters, to see if there ever is anything abnormal. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, some image operators (e.g. Image.FromFile) throw an OOM Exception when the data is not valid. I don't know what Server.MapPath is assumed to do, nor how valid its result is. Split the statement in simpler ones and debug. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                    M Offline
                    M Offline
                    Marc Clifton
                    wrote on last edited by
                    #9

                    Luc Pattyn wrote:

                    some image operators (e.g. Image.FromFile) throw an OOM Exception when the data is not valid.

                    Nice. 8 years later, and this post was quite relevant for me, in that it really wasn't an OOM error.

                    Latest Articles:
                    A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

                    L 1 Reply Last reply
                    0
                    • M Marc Clifton

                      Luc Pattyn wrote:

                      some image operators (e.g. Image.FromFile) throw an OOM Exception when the data is not valid.

                      Nice. 8 years later, and this post was quite relevant for me, in that it really wasn't an OOM error.

                      Latest Articles:
                      A Lightweight Thread Safe In-Memory Keyed Generic Cache Collection Service A Dynamic Where Implementation for Entity Framework

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      :rose: Today, I need to fix that statement; it should read:

                      some image operators (e.g. Image.FromFile) MAY throw an OOM Exception when the data is not valid.

                      There is no guarantee an OOM will occur on every damaged image! :)

                      Luc Pattyn [My Articles] The Windows 11 taskbar is a disgrace; a third-party add-on is needed to reverse the deterioration. I decline such a downgrade.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups