Rendering Sheet to image - WorksheetRender generates error - A Generic Error Occured in GDI+

Please see attached file and coding used.

static bool SaveExcelFile(string oldName)
{
bool saveStatus;

var newName = oldName.Replace("Inbox", "Batch");
var newFileInfo = new FileInfo(newName);
newName = newName.Replace(newFileInfo.Extension, "_pgnum.tif");

try
{
var imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions
{
ImageFormat = ImageFormat.Tiff,
HorizontalResolution = 200,
VerticalResolution = 200,
TiffCompression = Aspose.Cells.Rendering.TiffCompression.CompressionCCITT4,
OnePagePerSheet = true
};
var saveExcelWorkbook = new Aspose.Cells.Workbook(oldName);
var saveWorksheetColl = saveExcelWorkbook.Worksheets;
var wsCount = saveWorksheetColl.Count;

for (int ws = 0; ws < wsCount; ws++)
{
var sheet = saveWorksheetColl[ws];
if (sheet.Cells.Count == 0 || sheet.CodeName == "Look ups"
|| sheet.CodeName == "Instructions for use"
|| sheet.CodeName == "Internal Instructions"
|| sheet.CodeName == "Autouploader" || !sheet.IsVisible)
{
continue;
}
sheet.ViewType = Aspose.Cells.ViewType.NormalView;
var wsRender = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);
var saveName = newName.Replace("_pgnum", "_" + ws.ToString("00"));
wsRender.ToTiff(saveName);
}

saveStatus = true;
}
catch (Exception exc)
{
Console.WriteLine("Exception converting Excel Doc to TIF - {0}", exc.Message);
Log.Error(oldName, "Exception converting Excel Doc to TIF", exc.Message);
saveStatus = false;
}
GC.Collect();

return saveStatus;
}

Hi,

Thanks for using Aspose.Cells for .NET.

Please download and try the latest version:
Aspose.Cells
for .NET v7.3.2.3
. It will resolve your issue.

I have tested your issue with the latest version using the following code and it generated the tiff image fine.

I have attached the output image and screenshot for your reference.

C#


Workbook workbook = new Workbook(path);


Worksheet worksheet = workbook.Worksheets[“MASTER”];


//Apply different Image / Print options.

Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();

options.OnePagePerSheet = true;

options.ImageFormat = ImageFormat.Tiff;


SheetRender sr = new SheetRender(worksheet, options);


sr.ToTiff(path + “.out.tiff”);


Screenshot:

Hi,

We have looked into your issue further.

Since your worksheet is quite large, so entire image could not be placed in a single page. If it will be rendered on a single page, then the rendering will be too small to be visible.

So, please set reasonable Print Area. This may fix your issue.

i.e

wb.Worksheets[“MASTER”].PageSetup.PrintArea = “A1:W86”;

The latest version of Cells (version 7.3.2) was in use when error was received. Perhaps the difference between our versions was the fact that I was using the TiffCompression.CompressionCCITT4 whereas you were not using any specific TiffCompression setting. As the TiffCompression setting I am using is an imperative to our work, I cannot code using the default.

The spreadsheet size received (and consequently the number of sheets as well as the name of the sheets) is random depending on the customer that creates the workbook. If the size is smaller (as the majority of the received workbooks and spreadsheets typically are), automatically setting the print area to a generally large area as you suggested would unduly shrink the majority of the images being generated. Just the same, your suggestion is not without merit. I will set the render ToTiff method within a try catch block and set the print area whenever this error is generated.

Hi,

SuddathCoDeveloper:

The latest version of Cells (version 7.3.2) was in use when error was received. Perhaps the difference between our versions was the fact that I was using the TiffCompression.CompressionCCITT4 whereas you were not using any specific TiffCompression setting. As the TiffCompression setting I am using is an imperative to our work, I cannot code using the default.


I used the following code with CompressionCCITT4 tiff compression and it works fine. Please make sure that you are using the latest fix i.e.., v7.3.2.3 (Shakeel Faiz has shared the link in his previous post). The output tiff image is attached here.


Sample code:


string path = @"e:\test2\San+Francisco+CA+to+Beijing.xls";

Workbook workbook = new Workbook(path);



Worksheet worksheet = workbook.Worksheets["MASTER"];

workbook.Worksheets["MASTER"].PageSetup.PrintArea = "A1:W86";




//Apply different Image / Print options.


Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();


options.OnePagePerSheet = true;


options.ImageFormat = ImageFormat.Tiff;


options.TiffCompression = Aspose.Cells.Rendering.TiffCompression.CompressionCCITT4;


SheetRender sr = new SheetRender(worksheet, options);



sr.ToTiff(path + ".out1.tiff");



Thank you.

Hi,

We have fixed this issue.

Please download and try this fix: Aspose.Cells for .NET v7.3.2.4 and let us know your feedback.

C#


Workbook wb = new Workbook(@“e:\m\San+Francisco+CA+to+Beijing.xls”);

ImageOrPrintOptions iop = new ImageOrPrintOptions();

iop.ImageFormat = System.Drawing.Imaging.ImageFormat.Tiff;

iop.TiffCompression = TiffCompression.CompressionCCITT4;

iop.OnePagePerSheet = true;

iop.HorizontalResolution = iop.VerticalResolution = 200;

wb.Worksheets[“MASTER”].PageSetup.PrintArea = “A1:W86”;

WorkbookRender wr = new WorkbookRender(wb, iop);

wr.ToImage(“e:\m\San+Francisco+CA+to+Beijing.xls.tiff”);


Thank you for your time and effort. It works fine as long as the print area is set for rendering.

Hi,


Good to know that your issue is fixed!