Getting System.OutOfMemoryException: Out of memory When Save On Jpg File

Hi,
After updating aspose.pdf to version 11.6 with no changes in code,
When trying to convert jpg to pdf (Aspose.Pdf.Document.Save(string)), it randomally getting stuck, after some time i get the error:
System.OutOfMemoryException: Out of memory.
at System.Drawing.Bitmap…ctor(Stream stream)
at Aspose.Pdf.Image.(Double& , Double& , Double& , Boolean , MarginInfo , Double , Double , Page , List`1 , Boolean , Boolean , Boolean , Paragraphs& , Double )
at ​ .()
at Aspose.Pdf.Page.(Page )
at Aspose.Pdf.Document.ProcessParagraphs()
at Aspose.Pdf.Document.Save(Stream output)
at Aspose.Pdf.Document.Save(String outputFileName).

The PDF stays on 0kb and the process starts to inflate by its size like it’s on an endless loop.

Once i’m downgrading aspose version to 10.3 then it’s all ok, no error, no inflation, converted pdf file is ok.

hope for some help in this metter.
Thanks,
Tomer.

Hi Tomer,


Thanks for contacting support.

Can you please share the resource file, so that we can test the scenario in our environment. We are sorry for this inconvenience.

Hi,

Thanks for your reply,

Here is a file as example…

Thanks again,

Tomer

Hi Tomer,

Thanks for sharing the resource file.

I have tested the scenario using Aspose.Pdf for .NET 11.9.0 where I have used following code snippet and I am unable to notice any issue. As per my observations, the image file is properly being converted to PDF format. For your reference, I have also attached the output generated over my end.

[C#]

Document doc = new Document();

doc.Pages.Add();

Aspose.Pdf.Image image = new Aspose.Pdf.Image();

image.File = "c:/pdftest/5000.jpg";

doc.Pages[1].Paragraphs.Add(image);

doc.Save("c:/pdftest/Image_to_Pdf.pdf");

Hi codewarrior,

Thanks for your time and answer, I’m happy that there is no problem in the 11.9 version. I will try it.
Have you tried to test it also in the 11.6 version? This is problematic version .
As I mentioned before, also in the 10.3 version there is no problem.

Thanks,
Tomer.

Hi Tomer,


Thanks for sharing the details.

I have tested the scenario using Aspose.Pdf for .NET 11.6.0 and I am unable to notice any issue. As per my observations, the Image file is properly being converted to PDF format.

For your reference, I have also attached the output generated over my end.

Hi Shahbaz,
I downloaded 11.9 version, still getting out of memory exception after the process is climbing to over 1GB of memory.
My Code is:
private void HandleImages(string ext, string file, Image gPix)
{
using (Aspose.Pdf.Document newPdf = new Aspose.Pdf.Document())
{
int pages = 1;

            Stream stream = null;

            for (int j = 0; j < pages; j++)
            {
                stream = new System.IO.MemoryStream(System.IO.File.ReadAllBytes(file));
      gPix = new Bitmap(stream);
                Aspose.Pdf.Page page = newPdf.Pages.Add();
                
                // Set margins so image will fit, etc.
                page.PageInfo.Margin.Bottom = 0;
                page.PageInfo.Margin.Top = 0;
                page.PageInfo.Margin.Left = 0;
                page.PageInfo.Margin.Right = 0;

                double width, height;
                if (gPix.Width > page.PageInfo.Width || gPix.Height > page.PageInfo.Height)
                {
                    double dividePicBy = ((gPix.Height / page.PageInfo.Height) > (gPix.Width / page.PageInfo.Width)) ? (gPix.Height / page.PageInfo.Height) : (gPix.Width / page.PageInfo.Width);

                    width = gPix.Width / dividePicBy;
                    height = gPix.Height / dividePicBy;
                }
                else
                {
                    width = gPix.Width;
                    height = gPix.Height;
                }

                page.CropBox = new Aspose.Pdf.Rectangle(0, 0, width, height);
                // Create an image object
                Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
                // Add the image into paragraphs collection of the section
                page.Paragraphs.Add(image1);
                // Set the image file stream
                image1.ImageStream = stream;
            }

            newPdf.Save(file.Replace(ext, ".pdf"));
        }

}

The coded line which get “stuck” is :
newPdf.Save(file.Replace(ext, “.pdf”));

Hope this will help you recreate my problem… I’m still stuck at it.

Hi Tomer,


Thanks for sharing the details.

I have tested the scenario and have managed to reproduce same problem. For the sake of correction, I have logged it as PDFNET-41325 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Is there any update on this issue? I have the same problem with Aspose.PDF v17.8

@ntxl,

The linked ticket ID PDFNET-41325 is pending for the analysis and not resolved yet. Our product team will investigate as per their development schedules. We will let you know once a significant progress has been made in this regard.

@ts6 @ntxl

Thank you for being patient.

I would like to request you to please try using Aspose.Pdf for .NET 18.1 and add below lines of code in your program, OutOfMemoryException will not occur anymore.

page.SetPageSize(width, height); // Add this line
page.CropBox = new Aspose.Pdf.Rectangle(0, 0, width, height);

I hope this will be helpful. Please let us know if you need any further assistance.