locked
Out of Memory Error when Creating thumbnail of image with size more than 7000 pixcel RRS feed

  • Question

  • User-615990677 posted

    Hi Guies, I was doing wonders till I stucked with this error, can any body help.

    Issue is I am creating thumbnail of image on the fly and showing on my pages. When the image height/width exceed 7000 pixcel I get out of memory error. 

    I am attaching the code I am using

     

    using System;

    using System.Data;

    using System.Configuration;

    using System.Collections;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.Drawing;

    using System.Drawing.Imaging;

    public partial class CreateThumbNail : System.Web.UI.Page

    {

    private void Page_Load(object sender, System.EventArgs e)

    {

    string Image = Request.QueryString["Image"];if (Image == null)

    {

     this.ErrorResult();

    return;

    }

     

     

    string sSize = Request["Size"];

    int Size = 120;

    if (sSize != null)Size = Int32.Parse(sSize);

     

     

    string Path = Server.MapPath(Request.ApplicationPath) + "\\" + Image;Bitmap bmp = CreateThumbnail(Path, Size, Size);

     

     

    if (bmp == null)

    {

    Response.Write(
    "Bmp is null");

    Response.End();

    // this.ErrorResult();

    return;

    }

     

     

     

    // Put user code to initialize the page here

    Response.ContentType = "image/jpeg";

    bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

    bmp.Dispose();

    }

     

     

    private void ErrorResult()

    {

    Response.Clear();

    Response.StatusCode = 404;

    Response.End();

    }

     

     

    ///

    /// Creates a resized bitmap from an existing image on disk.

    public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)

    {

    System.Drawing.
    Bitmap bmpOut = null;

    //try

    //{

    Bitmap loBMP = new Bitmap(lcFilename);

    ImageFormat loFormat = loBMP.RawFormat;

     

    decimal lnRatio;

    int lnNewWidth = 0;

    int lnNewHeight = 0;

     

     

    //*** If the image is smaller than a thumbnail just return it

    if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)return loBMP;

     

     

     

     

    if (loBMP.Width > loBMP.Height)

    {

    lnRatio = (
    decimal)lnWidth / loBMP.Width;

    lnNewWidth = lnWidth;

    decimal lnTemp = loBMP.Height * lnRatio;lnNewHeight = (int)lnTemp;

    }

    else

    {

    lnRatio = (
    decimal)lnHeight / loBMP.Height;

    lnNewHeight = lnHeight;

    decimal lnTemp = loBMP.Width * lnRatio;

    lnNewWidth = (int)lnTemp;

    }

     

     

    bmpOut = new Bitmap(lnNewWidth, lnNewHeight);

    Graphics g = Graphics.FromImage(bmpOut);

    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

    g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);

    g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);

     

    loBMP.Dispose();

    //}

    //catch

    //{

    // return null;

    //}

     

     

    return bmpOut;

    }

    }

     

    Wednesday, February 18, 2009 8:13 AM

All replies

  • User-1509636757 posted

    string Path = Server.MapPath(Request.ApplicationPath) + "\\" + Image;
     

    check this path validity. i mean whether file is exists there or not..

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

     Referenced Link > Windows Service - Out of memory - .NET

    hope it helps./.

    Wednesday, February 18, 2009 8:33 AM
  • User-615990677 posted

    Thanks for reply

    But the image does exist. If I just decrease the size of the image, thumbnail will start genrating. All the images with size more than 7000 pixcels the error arises.

    Wednesday, February 18, 2009 11:16 AM
  • User-1509636757 posted

     can you please look at the innerexception and post the details?

    Wednesday, February 18, 2009 12:28 PM
  • User-615990677 posted

     Can you please elobarate little more, well I am giving the line specific error

     

    Exception Details: System.OutOfMemoryException: Out of memory.

    g.DrawImage(loBMP, 0, 0, Convert.ToInt32(CurrentimgWidth), Convert.ToInt32(CurrentimgHeight))

    IS this what you were asking for.

     

     

     
     

    Monday, February 23, 2009 5:43 AM