Out of Memory exception when saving Excel as grayscale TIFF

I’m trying to convert an Excel spreadsheet to a grayscale TIFF using the following code:

      var fileStream = File.OpenRead("SomeTestFile.xlsx");
      var excel = new Workbook(fileStream);

      var options = new ImageSaveOptions(SaveFormat.TIFF);

      options.ImageOrPrintOptions.HorizontalResolution = 200;
      options.ImageOrPrintOptions.VerticalResolution = 200;
      options.ImageOrPrintOptions.TiffCompression = TiffCompression.CompressionLZW;
      options.ImageOrPrintOptions.PixelFormat = PixelFormat.Format16bppGrayScale;

      var memoryStream = new MemoryStream();

      excel.Save(memoryStream, options);

When I run this I get an Aspose.Cells.CellsException : Out of memory. The test Excel file is only 50kb and I have plenty of memory. If I run the same code using PixelFormat.Format16bppRgb555 or PixelFormat.Format24bppRgb it works fine. I am also able to use TiffCompression.CompressionCCITT3 to get a bitonal image, but I would like a grayscale image. For now I’m just converting the TIFF to grayscale after saving, but it adds a little extra time to the process.


I’m using the .NET 3.5 ClientProfile v8.7.1.0 (due to the issues with CAS and GDI+ as noted here: Aspose Cells security critical exception) Thanks for any help.

Hi Walter,


Thank you for contacting Aspose support.

We have evaluated the presented scenario while using the latest version of Aspose.Cells for .NET 8.7.1.1 and following piece of code against a sample of my own. We are able to replicate the CellsException: Out of memory while saving the Workbook to TIFF in grayscale therefore we have logged this incident as CELLSNET-44275 for further investigation. Please spare us little time to properly analyze the problem cause and get back with updates in this regard.

Great, thanks very much. If it makes any difference, we are current customers for several Aspose products. If it would be helpful I can retrieve our licensing information.

Hi Walter,


We do not require your licensing information to provide the fix for the mentioned issue. We will share investigation results here as soon as we have completed the preliminary analysis.

Hi,


We have evaluated your issue further.
Please refer to the page:
https://bytes.com/topic/c-sharp/answers/278572-out-memory-graphics-fromimage it seems that PixelFormat16bppGrayScale is not supported by GDI+. So, you can set BlackAndWhite to true in PageSetup of worksheet before rendering to get a gray scale image:
e.g
Sample code:

var book = new Workbook(srcFile);
WorksheetCollection sheets = book.Worksheets;
foreach (Worksheet sheet in sheets)
{
sheet.PageSetup.BlackAndWhite = true;
}

Hope, this helps a bit.

Thank you.

Thanks for the information, that helps. I’ll continue using my grayscale converter after exporting, since it drops the file size quite a bit.