We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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()

@mhopkins,
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

Thanks
Andy.

@weissa,
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.

AsposeChartToImageIssue.zip (5.3 MB)
Ahsan,

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

Thanks,

Matt Hopkins

@mhopkins,
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