Using Chart.ToImage in Aspose.Cells for .NET Version 21.7 failing with "'System.InvalidOperationException: 'Failed to compare two elements in the array.''

Please see the following code and stack trace.

	public bool GetChartImage(string address, Stream stream, int scale)
		if (scale == 0) scale = 100;

		// Try to get the chart by the name
		Chart chart = Worksheet.Charts[address];
		if (chart == null)
			// See if address is a cell location and there's a chart name in that cell location?
			var chartName = GetRangeValueOrNull(address);
			chart = Worksheet.Charts[chartName];

			if (chart == null)
				return false;

		var saveHeight = chart.ChartObject.HeightScale;
		var saveWidth = chart.ChartObject.WidthScale;

		var options = new ImageOrPrintOptions()
			ChartImageType = ImageFormat.Png,
			ImageType = Aspose.Cells.Drawing.ImageType.Png
		chart.ChartObject.HeightScale = Math.Min(400, Math.Max(10,scale));
		chart.ChartObject.WidthScale = scale;
		chart.ToImage(stream, options);

		chart.ChartObject.HeightScale = saveHeight;
		chart.ChartObject.WidthScale = saveWidth;
		return true;

at System.Array.SorterObjectArray.IntrospectiveSort(Int32 left, Int32 length)
at System.Array.Sort(Array keys, Array items, Int32 index, Int32 length, IComparer comparer)
at System.Collections.ArrayList.Sort(Int32 index, Int32 count, IComparer comparer)
at .()
at .( , Rectangle , )
at .( )
at . ()
at .(Boolean )
at . (Stream , Chart )
at Aspose.Cells.Charts.Chart.ToImage(Stream stream, ImageOrPrintOptions options)
at QueBIT.ReportWORQ.Engine.Excel.AsposeExcelWorksheet.GetChartImage(String address, Stream stream, Int32 scale) in C:\sc\ReportWORQ\QueBIT.ReportWORQ.Engine\Excel\AsposeExcelWorksheet.cs:line 132
at QueBIT.ReportWORQ.Engine.Generation.RuntimeReport.Run(IReportCalculateService rptCalcService, IExcelApplication xlApplication, IReportTelemetry reportTelemetry, StreamFactory streamFactory, JobStepStatus stepStatus) in C:\sc\ReportWORQ\QueBIT.ReportWORQ.Engine\Generation\RuntimeReport.cs:line 208
at QueBIT.ReportWORQ.Engine.Generation.RuntimeJobStep.<>c__DisplayClass78_1.g__RunReportBody|2(IRuntimeReport rpt) in C:\sc\ReportWORQ\QueBIT.ReportWORQ.Engine\Generation\RuntimeJobStep.cs:line 124
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.b__1()

Please share a sample file that can be used to test this code. Also identify the “address” argument value if there are multiple charts in the sample file.

Address is the name of the chart. All you need to reproduce this is:

Chart chart = Worksheet.Charts[name];
var options = new ImageOrPrintOptions()
ChartImageType = ImageFormat.Png,
ImageType = Aspose.Cells.Drawing.ImageType.Png

chart.ToImage(stream, options);

This same code works in version 20.12 but fails in 21.6 & 21.7


I have tried it with a sample Excel file having chart but could not observe any issue using versions 21.7 and 21.6. You may please share your sample file for our testing as without reproducing the issue it is not possible to identify the issue and assist further.

Also share your environment details for our reference. (5.3 MB)

Please see the attached solution for a demonstration of the issue.


Matt Hopkins

We have observed the issue and logged it in our database for further investigation. You will be notified here once any update is ready for sharing.

This issue is logged as:
CELLSNETCORE-131 - Using Chart.ToImage raises exception with version 21.7

Hi @mhopkins, @weissa
Please try the latest fix 21.7.6.
Aspose.Cells21.7.6 For .Net2_AuthenticodeSigned.Zip (5.6 MB)
Aspose.Cells21.7.6 For .Net4.0.Zip (5.6 MB)
Aspose.Cells21.7.6 For .NetStandard20.Zip (5.6 MB)

We fixed the issue of reporting exceptions and now render the image as in 20.12.

A post was split to a new topic: Aspose.Cells for .NET 21.7.6 Needed

Aspose Team. All looks good. Our unit tests are passing and the Chart.ToImage issue is resolved. Thank you as always for the wonderful support.

You are welcome.