locked
Out of Memory Exception RRS feed

  • Question

  • User2103134756 posted

    Trying to display photos by paging through an array of objects with a path to the image. I want to re-dimension the photo on the fly based on the aspect ratio with CSS. I am using the Page_PreRender even and disabling the buttons so the user can't click before a session variable with an index can be initialized or the I get a null value error. I'm not instantiating the image and every click to the next button is a postback. I think it might be because I'm constantly bringing up images with every click. Here is the code...

    protected void Page_PreRender(object sender, EventArgs e)
        {
    
            imgPhoto.ImageUrl = GetNextPhoto();
            string path = HttpContext.Current.Server.MapPath(imgPhoto.ImageUrl);
            int width = System.Drawing.Image.FromFile(path).Width;
            int height = System.Drawing.Image.FromFile(path).Height;
            if (width > height)
                imgPhoto.CssClass = "photoLandscape";
            else
                imgPhoto.CssClass = "photoPortrait";
    
            lbNext.Enabled = true;
            lbPrevious.Enabled = true;
        }

    Tuesday, April 17, 2018 11:11 PM

Answers

  • User753101303 posted

    Hi,

    FromFile does instantiate an image. I would use something such as :

    using(var img=System.Drawing.Image.FromFile(path))
    {
       width = img.Width;
       height = img.Height;
    } // so that the image is disposed

     It's best to be explicit about where it happens (you have this error on FromFile ???). Also could it be that it happens on a particular file ? Finally usually the image is really resized. Seems you size the image using CSS but you may end up in downloading big images to just then show them with a smaller size while you could have saved some bandwidth.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 18, 2018 7:13 AM

All replies

  • User283571144 posted

    Hi oneillj,

    As far as I know, the out of memory exception means there is not enough memory to continue the execution of a program.

    According to your codes,I couldn't find the codes which will cause the out of memory exception.

    I suggest you could post more details codes about your issue.

    For example, which line throw the error message.

    If you could post more detail information, it will be more easily for us to find the solution.

    Best Regards,

    Brando

    Wednesday, April 18, 2018 6:44 AM
  • User753101303 posted

    Hi,

    FromFile does instantiate an image. I would use something such as :

    using(var img=System.Drawing.Image.FromFile(path))
    {
       width = img.Width;
       height = img.Height;
    } // so that the image is disposed

     It's best to be explicit about where it happens (you have this error on FromFile ???). Also could it be that it happens on a particular file ? Finally usually the image is really resized. Seems you size the image using CSS but you may end up in downloading big images to just then show them with a smaller size while you could have saved some bandwidth.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 18, 2018 7:13 AM
  • User2103134756 posted

    Sorry about the lack of info. Throws on the first Image.FromFile(). 

    Wednesday, April 18, 2018 8:02 PM
  • User2103134756 posted

    I was wondering if there was a way to release the image. I will give that a try. I've optimized the images to where they are all well under 1mb. This only happens when it is uploaded to the hosting site.

    Wednesday, April 18, 2018 8:03 PM
  • User2103134756 posted

    Looks like that was it PatriceSC. Thanks for the answer. 

    Wednesday, April 18, 2018 9:20 PM