0

We are drawing and editing pdf by using the following code,but when we play with 11mb size file,we are getting out of memory exception,can we fix the issue,I had used System.Drawing.Image.FromFile instead of FromStream but no luck..

 public static string ImageCar()
    {

        string FileName = HttpContext.Current.Session["filename"].ToString();
        Document doc = new Document(FileName);

        ArrayList arrFiles = new ArrayList();
        string strFileName = "";
        for (int pageCount = 1; pageCount <= TotalPages; pageCount++)
        {
            using (FileStream imageStream = new FileStream(HttpContext.Current.Server.MapPath("Input/image_" + strDateTime + "_" + pageCount + ".png"), FileMode.Create, FileAccess.ReadWrite))
            {
                strFileName = HttpContext.Current.Server.MapPath("Path" + strDateTime + "_" + pageCount + ".png");
                arrFiles.Add(strFileName);

                PngDevice pngDevice = new PngDevice();
                //Convert a particular page and save the image to stream
                pngDevice.Process(doc.Pages[pageCount], imageStream);
                using (System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream))
                {
                    ScaleImage(image, 1189, 835, HttpContext.Current.Server.MapPath("Input/image1_" + strDateTime + "_" + pageCount + ".png"), out height, out Aratio);
                    image.Dispose();
                    imageStream.Close();
                    if (pageCount == 1)
                        fields = CheckFields(doc, pageCount, "image1_" + strDateTime + "_" + pageCount + ".png", fields, Convert.ToDouble(Aratio), licensed);

                    pages = pages + "," + "image1_" + strDateTime + "_" + pageCount + ".png";
                    Ratios = Ratios + "," + Aratio;
                    Allheights = Allheights + "," + height;

                    // Delete file from image folder
                    try
                    {
                        if (File.Exists(strFileName))
                        {
                            File.Delete(strFileName);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }

        Ratios = Ratios.Substring(1, Ratios.Length - 1);
        pages = pages.Substring(1, pages.Length - 1);
        Allheights = Allheights.Substring(1, Allheights.Length - 1);
        if (fields != "")
        {
            fields = fields.Substring(3, fields.Length - 3);
        }

        return pages + "%#" + Ratios + "%#" + Allheights + "%#" + fields;
    }
2
  • Bear in mind that OutOfMemoryException is System.Drawings favourite exception. If anything causes an error, it will attempt to decode the error into one of a few distinct failure modes but if it doesn't match any of those, it'll tend to through OOM - even if the actual error had nothing to do with memory. Commented Feb 21, 2018 at 12:49
  • Don't dispose/close stuff inside using blocks... the using blocks are there exactly to automate that stuff. Commented Mar 1, 2018 at 23:39

1 Answer 1

1

System.Drawing has known memory leaks in a ASP site, and should never be used. It will eventually case OOM errors, and while you can avoid it by restarting the server every so often, or throwing more memory at it, there is no real, long term fix. Use some other library for the image manipulation portion, and you should have less issues.

See the bottom of this page: https://msdn.microsoft.com/en-us/library/system.drawing(v=vs.110).aspx

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. For a supported alternative, see Windows Imaging Components.

Sign up to request clarification or add additional context in comments.

3 Comments

is there any way we can treat data as junks in memory or store it in hard disc???
If you don't scale the image then it would be fine. But scaling is the issue if using that namespace
@peter if this solved your issue, could you please mark the answer as such. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.