How to decrease memory using during the page.Graphics.DrawImage

Hi

I'm generation PDF with 40+ images (2Kx2K resolution):

```
var document = new PdfDocument();
document.Compression = PdfCompressionLevel.Best;
document.EnableMemoryOptimization = true;

//...

for (var i=0; i< 40; i++) {
    var imageStream = ...;
    var centerImage = PdfImage.FromStream(imageStream);
    var centerImagePosition = ...;
    var centerImageNewSize = new SizeF(400, 250)
    page.Graphics.DrawImage(centerImage, centerImagePosition, centerImageNewSize); // OOM exception here; memory 1G+
    imageStream.Dispose();
}

await document.SaveAsync(file);
document.Dispose();
```

as result I receive an exception:

```
2021-01-20 23:48:06.511 +03:00 [ERR] Error during export to PDF
System.NullReferenceException: Object reference not set to an instance of an object.
   at Syncfusion.Pdf.Graphics.PdfBitmap.SetColorSpace()
   at Syncfusion.Pdf.Graphics.PdfBitmap.Save()
   at Syncfusion.Pdf.Graphics.PdfGraphics.DrawImage(PdfImage image, Single x, Single y, Single width, Single height)
   at Syncfusion.Pdf.Graphics.PdfGraphics.DrawImage(PdfImage image, PointF point, SizeF size)
   at MySuperApp.Uwp.Services.PdfExport.Templates.ProductTemplate.DrawAsync()
   
2021-01-20 23:48:31.765 +03:00 [FTL] OnUnhandledException
System.AggregateException: One or more errors occurred. (Insufficient memory to continue the execution of the program.) ---> System.OutOfMemoryException: Insufficient memory to continue the execution of the program.
   at Syncfusion.Pdf.Graphics.PdfBitmap.SaveImageByFormat()
   at Syncfusion.Pdf.Graphics.PdfBitmap.<SaveImage>b__50_0()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Syncfusion.Pdf.Graphics.PdfBitmap.SaveImage()
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__7_0(Object state)
   at System.Threading.WinRTSynchronizationContextBase.Invoker.InvokeCore()
---> (Inner Exception #0) System.OutOfMemoryException: Insufficient memory to continue the execution of the program.
   at Syncfusion.Pdf.Graphics.PdfBitmap.SaveImageByFormat()
   at Syncfusion.Pdf.Graphics.PdfBitmap.<SaveImage>b__50_0()<---

```

How can I decrease memory usage during image drawing?

Thanks

8 Replies 1 reply marked as answer

GK Gowthamraj Kumar Syncfusion Team January 22, 2021 06:01 PM UTC

Hi Yauheni, 

Thank you for contacting Syncfusion support. 

Issues 
Comments 
System.NullReferenceException: Object reference not set to an instance of an object. 
We have tried to reproduce the reported exception with different resolution image samples in our end. We suspect this exception may occurs only in particular image specific. 
Kindly please provide more details such as particular image to reproduce the exception in our end. So, that it will be helpful for us to analyze and assist you further on this. 

System.AggregateException: One or more errors occurred. (Insufficient memory to continue the execution of the program.) ---> System.OutOfMemoryException: Insufficient memory to continue the execution of the program. 
We have checked the reported issue with the provided details in our end. We can able to reproduce the issue, while using the 2k x 2k resolution images. Currently, we are validating on this and we will update the further details on January  27th 2021. 

Regards, 
Gowthamraj K 



YP Yauheni Pakala January 25, 2021 09:27 PM UTC

I'm using Syncfusion.Pdf.UWP v18.1.0.59


> please provide more details such as particular image to reproduce the exception in our end

Attached archive:

Type: JPEG
Size: 1.04
Dimensions: 2048x1365



Attachment: img_b925e718.zip


SL Sowmiya Loganathan Syncfusion Team January 26, 2021 01:14 PM UTC

Hi Yauheni,  
 
Thank you for the image file.  
 
System.NullReferenceException: Object reference not set to an instance of an object. 
 
Attached archive: 
 
Type: JPEG 
Size: 1.04 
Dimensions: 2048x1365 
  
We have tried to convert the given image to PDF document in UWP application in the product version 18.1.0.59. But we regret to let you know that we were unable to reproduce it. Please find the sample which we have tried from below,  
 
 
Please revert us with the below details, it will helpful for us to provide the precise solution on this.  
 
  • Modified sample
  • Environment details (i.e., OS)
 
 
Regards, 
Sowmiya Loganathan 



GK Gowthamraj Kumar Syncfusion Team January 27, 2021 03:30 PM UTC

Hi Yauheni, 

Thank you for your patience. 

We have checked the reported “OutOfMemory exception issue” by simply read and writing that large PDF document stream multiple times without our library and memory gets peeks for each read and writing that large stream. On further analysis, we found that the issue occurs only in x86 bit .NET Framework due to less memory allocation when compared with x64 bit framework in UWP and so it works properly without any issue in x64 bit framework. It seems to limitation for memory allocation in x86 bit Framework. So we suggest you to change the .NET framework settings to x64 bit to resolve the exception issue. 

Regards, 
Gowthamraj K 



YP Yauheni Pakala January 28, 2021 09:36 PM UTC

Hi, thanks! x64 works fine.

Verified on presentation with 100 slides. PDF (173 pages) was generated successfully.

What about ARM?





GK Gowthamraj Kumar Syncfusion Team January 29, 2021 12:25 PM UTC

Hi Yauheni, 
 
Thank you for your update. We are glad to know that your problem has been solved. 
 
Currently, we are checking on your requirement in ARM devices and we will update the details on February 2nd 2021. 
 
Regards, 
Gowthamraj K 



GK Gowthamraj Kumar Syncfusion Team February 2, 2021 04:31 PM UTC

Hi Yauheni, 

Currently, we are setting the environment to analyse this requirement in ARM device and we will update the further details on February 04th ,2021. 

Regards, 
Gowthamraj K 



SL Sowmiya Loganathan Syncfusion Team February 4, 2021 01:37 PM UTC

Hi Yauheni,  
 
We have checked reported issue in ARM frameworks and it works find without any issues. However the reported exception may occurs while drawing the images with high resolution. We can resolve this issues to resize the images before the drawing to the pdf page. We have created the simple sample to resize the images and please find download link for the same from below,   
  
 
Note: Still if you have facing any issues, please check with ARM settings. 
  
Please try the above sample in your end and let us know the result.  
 
Regards, 
Sowmiya Loganathan 
 


Marked as answer
Loader.
Up arrow icon