Free Support Forum - aspose.com

SheetToImage System.NullReferenceException: Object reference not set to an instance of an object

I have a very simple xls file that I would like to manipulate and save as an image.

var workbook = new Workbook();
workbook.Open(“Blank.xls”);

var worksheet = workbook.Worksheets[0];
worksheet.Cells.InsertRows(10, 20);

workbook.Worksheets[0].SheetToImage(“Blank.jpg”, ImageFormat.Jpeg);

The above code used to work in version 4.9.0.2. After upgrading to 4.9.1.3 we started getting a System.NullReferenceException: Object reference not set to an instance of an object error. We upgraded to 5.0.0.2 and still the same error.

Can this be fixed? If so, can it be applied to the 4.9.0.x series? We find that whenever a new fix is made, that something else break.

Thanks

Harry

Hi Harry,

Please try the attached 5.0.0.7 fix of Aspose.Cells for .NET.

Please use the following code to render the sheet to the image instead of using Worksheet.ToImage().

Workbook workbook = new Workbook();
workbook.Open(@"d:\cells500\Blank.xls ");
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells.InsertRows(10, 20);
Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions();
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
imgOptions.VerticalResolution = 200;
imgOptions.HorizontalResolution = 200;
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
Worksheet sheet = workbook.Worksheets[i];
Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);
int sheetPageCount = sr.PageCount;
for (int j = 0; j < sheetPageCount; j++)
{
sr.ToImage(j, @“d:\cells500\Blank_image” + i.ToString() + “_” + j.ToString() + “.jpeg”);
}
}

Thank you

The new dll with the code above works.

Does that mean that the worksheet.SheetToImage method is not supported anymore?

As mentioned in my original post, is there anyway this issue can be fixed in the 4.9.0.x series? Our concern is that whenever we upgrade to a new version, something else breaks.

Hi,

Well, we might exclude the SheetToImage() method in future versions as it will be replaced by SheetRender API which is enhanced or reliable. The Worksheet.SheetToImage method is not enhanced any more. So, you should use SheetRender API.

Moreover, we cannot add functionality or fix the issue in v4.9.0.x or any older version for any feature as we cannot go against our internal policies. So, I am afraid, you need to use later/recent versions of the product.

Thank you.