Save to memory stream: A generic error occurred in GDI+

I've created a workbook and sheet in XLsIO and am trying to save it to an image file. After creating the sheet, I execute this code:

Dim stream As New IO.MemoryStream
Dim imgProjStatus As Drawing.Image = sheet.ConvertToImage(9, 1, rw, 13)
imgProjStatus.Save(sFileName, Drawing.Imaging.ImageFormat.Png)
imgProjStatus.Dispose()


Randomly, I get a "A generic error occurred in GDI+" error on the "Save" method. The "save" actually works all of the time, but about 1/3 of the time, the "GDI" error is generated - 2/3 of the time, no error is generated. 

If I ignore the error (Try/Catch with no code in the "catch"), the user isn't aware that anything happened - everything works as planned.

There must be a better way than to just ignore the error. 

3 Replies

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team March 27, 2020 09:11 AM UTC

Hi Bert, 

Greetings from Syncfusion. 

We have tried to reproduce the reported issue at our end. On further analysis, we found that the reason for error might be invalid path. Please look into the following link to know more about this. 


We have prepared a simple working sample and the same can be downloaded from the following link. 


Please find the code snippet below. 

Code Snippet:  

using (ExcelEngine excelEngine = new ExcelEngine()) 
{ 
  IApplication application = excelEngine.Excel; 
  application.DefaultVersion = ExcelVersion.Excel2016; 
 
  //Create a workbook 
  IWorkbook workbook = application.Workbooks.Create(1); 
  IWorksheet worksheet = workbook.Worksheets[0]; 
  worksheet.Range["A1:E5"].Text = "Sample"; 
 
  System.Drawing.Image imgProjStatus = worksheet.ConvertToImage(1, 1, 5, 5); 
  imgProjStatus.Save(Server.MapPath("App_Data/Sample.png"), System.Drawing.Imaging.ImageFormat.Png); 
  imgProjStatus.Dispose(); 
} 

Kindly try the sample and let us know if it helps. Else, please share us the issue reproducing sample, which will be helpful for us to investigate further and provide prompt solution at the earliest.  

Also, please let us know the Syncfusion XlsIO version you are using at your end. 

Regards, 
Keerthi. 



BS Bert Sirkin March 27, 2020 05:24 PM UTC

The issue isn't an invalid path. I think the issue is that the GDI dll is locking the file once it's created (see https://stackoverflow.com/questions/8905714/overwrite-existing-image), but it also appears tha it doesn't lock it every time. If I try to create the same PNG file name over and over, I only get the issue occasionally. I tried deleting the file if it already exists before re-creating it but that met with an error indicating that another process had the file locked, and I couldn't delete it.

If I use a unique filename every time, I never get the issue - the path is always the same, only the filename is changing, not the path.

So, the issue appears to be the "save" of the Drawing.image is causing the file to be locked.

For now, I will live with the solution of using a unique filename each time I save the image.



KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team March 30, 2020 08:49 AM UTC

Hi Bert, 

Thanks for the update and we are glad that you have found a solution. 

Kindly share us the issue reproducing sample which will be helpful for us to look into the query.  

Regards, 
Keerthi. 


Loader.
Up arrow icon