Out of memory while cropping PDF page

I am trying to crop multiple areas of the PDF page and create a new pdf file for each area. In production It works 90% of the time but sometime we get System.OutOfMemoryException: Out of memory. exception
We do have enough memory on the server and the server does not run out of memory. I was also able to re-produce the issue on local machine for a single document.

So its not out of memory error as the message says.

The code below when i executed in .NET integration test and i was able to re-produce the issue. Its throwing exception while saving 2nd out2.pdf

( Note that however same code if i execute in a separate console application with the same test file it does not throw exception. Weird)

I am using Aspose.PDF version 11.7.0.0

   public class Cropper
    {
        public void Crop()
        {
            var sourceFile = "d:\\temp\\TestFile.pdf";

            var boxes = new CartesianCropBox2[]
            {
                new CartesianCropBox2(){LLX=0,LLY=333,URX=611,URY=790,Height=457,Width=611 },
                new CartesianCropBox2(){LLX=0,LLY=219,URX=611,URY=333,Height=114,Width=611 },
                new CartesianCropBox2(){LLX=0,LLY=102,URX=611,URY=219,Height=117,Width=611 }
            };


            using (var sourcePdf = new Document(sourceFile))
            {
                var sourcePage = sourcePdf.Pages[1];
                var header = GetCroppedImage(sourcePage, boxes[0].LLX, boxes[0].LLY, boxes[0].URX, boxes[0].URY, boxes[0].Height, boxes[0].Width);
                Aspose.Pdf.Image image = null;
                try
                {
                    image = GetCroppedImage(sourcePage, boxes[1].LLX, boxes[1].LLY, boxes[1].URX, boxes[1].URY, boxes[1].Height, boxes[1].Width);
                    using (var doc1 = new Document())
                    {
                        var newPage = doc1.Pages.Add();

                        newPage.PageInfo.Margin.Left = 36;
                        newPage.PageInfo.Margin.Right = 36;

                        newPage.Paragraphs.Add(header);
                        newPage.Paragraphs.Add(image);
                        doc1.Save("d:\\temp\\out1.pdf");

                    }
                }
                finally
                {
                    image.ImageStream.Dispose();
                    //image.ImageStream = null;
                }               

                try
                {
                    image = GetCroppedImage(sourcePage, boxes[2].LLX, boxes[2].LLY, boxes[2].URX, boxes[2].URY, boxes[2].Height, boxes[2].Width);
                    using (var doc2 = new Document())
                    {
                        var newPage = doc2.Pages.Add();

                        newPage.PageInfo.Margin.Left = 36;
                        newPage.PageInfo.Margin.Right = 36;

                        newPage.Paragraphs.Add(header);
                        newPage.Paragraphs.Add(image);

                        doc2.Save("d:\\temp\\out2.pdf");
                    }
                }
                finally
                {
                    image.ImageStream.Dispose();
                    //image.ImageStream = null;
                }

                header.ImageStream.Dispose();
            }
        }

        private Aspose.Pdf.Image GetCroppedImage(Aspose.Pdf.Page sourcePage, double llx, double lly, double urx, double ury, double height, double width)
        {
            Aspose.Pdf.Image img = new Aspose.Pdf.Image();
            using (var tDoc = new Document())
            {
                tDoc.Pages.Add(sourcePage);

                Aspose.Pdf.Rectangle pageRect = new Rectangle(llx, lly, urx, ury);
                tDoc.Pages[1].CropBox = pageRect;

                img.FixHeight = height;
                img.FixWidth = width;
                img.Margin.Bottom = 20;

                //JpegDevice imgDevice = new JpegDevice(new Resolution(1024, 768));
                JpegDevice imgDevice = new JpegDevice(new Resolution(1920, 1080));
                img.ImageStream = new MemoryStream();

                // Convert a particular page and save the image to stream
                imgDevice.Process(tDoc.Pages[1], img.ImageStream);
                img.ImageStream.Seek(0L, SeekOrigin.Begin);
            }

            return img;
        }

        private class CartesianCropBox2
        {
            public double LLX { get; set; }
            public double LLY { get; set; }
            public double URX { get; set; }
            public double URY { get; set; }
            public double Height { get; set; }
            public double Width { get; set; }
            public bool ContinueOnNextPage { get; set; }
        }
    }

The only way i was able to solve the issue is by reducing the resolution. But i would like to keep the resolution high

Error

" at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)\r\n at Aspose.Pdf.XImageCollection.\u0002(Stream \u0002, \u0003​ \u0003, \u0002 \u0005, Int32 \b, Image& \u0006)\r\n at Aspose.Pdf.XImageCollection.\u0002(Int32 \u0002, Stream \u0003, Int32 \u0005, Boolean \b, ImageFilterType \u0006, Boolean \u000e)\r\n at Aspose.Pdf.XImageCollection.\u0002(Stream \u0002, Int32 \u0003, Boolean \u0005, ImageFilterType \b, Boolean \u0006)\r\n at Aspose.Pdf.Page.\u0002(Stream \u0002, Rectangle \u0003, CompositingParameters \u0005, Boolean \b, Boolean \u0006)\r\n at Aspose.Pdf.Image.\u0002(Double& \u0002, Double& \u0003, Double& \u0005, Boolean \b, MarginInfo \u0006, Double \u000e, Double \u000f, Page \u0002 , List`1 \u0003 , Boolean \u0005 , Boolean \b , Boolean \u0006 , Paragraphs& \u000e , Double \u000f )\r\n at \u000f .\u0005()\r\n at Aspose.Pdf.Page.\u0003(Page \u0002)\r\n at Aspose.Pdf.Document.ProcessParagraphs()\r\n at Aspose.Pdf.Document.Save(Stream output)\r\n at Aspose.Pdf.Document.Save(String outputFileName)\r\n at Domain.IntegrationTests.BackgroundServices.Cropper.Crop() in xxxxxxxxxxxxxx\IntegrationDomain\Tests\Domain.IntegrationTests\BackgroundServices\Cropper.cs:line 67\r\n at Domain.IntegrationTests.BackgroundServices.IShreddingServiceTests.SplitDocumentUsingYAxis2() in xxxxxxxxxxxxxx\IntegrationDomain\Tests\Domain.IntegrationTests\BackgroundServices\IShreddingServiceTests.cs:line 84"

Capture.PNG (14.4 KB)

@Laksh

Thank you for contacting support.

Please note that support is provided based on latest available version because it includes more features and bug fixes. Kindly upgrade to Aspose.PDF for .NET 19.9 and then share your kind feedback with us.

Moreover, you may consider applying for a free 30-days temporary license with which you can evaluate the API without any restrictions. In case you need further assistance, please feel free to let us know.