Chart ToImage - legend text sometimes is messed up

I have an Excel file with chart, generated with Aspose.Cells. Then I get the chart and convert it to an image:

byte[] excelFile....
using var memoryStream = new MemoryStream(excelFile);
using Workbook excelWorkbook =
    new Workbook(memoryStream, new LoadOptions(LoadFormat.Xlsx));
var chart = excelWorkbook.Worksheets[chartWorksheetIndex].Charts[0];
using var stream = new MemoryStream();
chart.ToImage(stream, imgProps);
using var chartImage = new Bitmap(stream);

In the image, sometimes the legend text is moved/aligned to left or right:
screenshots.zip (163.2 KB)

I’m using Aspose.Cells version 22.1.0.

I’ll appreciate any help or additional info for the issue.

@hvasilev,

Thanks for the screenshots.

Please try using our latest version/fix: Aspose.Cells for .NET v22.8 (Download | NuGet).

If you still find the issue with latest version/fix, kindly do share your Excel file(s) containing the chart, we will check it soon.

PS. please zip the files prior attaching.

I added the files to .zip.

@hvasilev,

You attached zipped archive contains only screenshots. I requested you to provide MS Excel files (containing the charts), so we could evaluate your issue on our end. Please do the needful and zip the Excel files and attach the zipped archive(s) here.

@Amjad_Sahi,

I added the Excel files in xlsx_files.zip (26.5 KB).

@hvasilev,

Please try our latest version/fix: Aspose.Cells for .NET v22.8 (Download | NuGet) . I have tested your scenario/case with latest version using the following sample code with your template files and it works fine:
e.g.
Sample code:

[1]  
           byte[] excelFile = File.ReadAllBytes("e:\\test2\\Segmentation.xlsx");
           var memoryStream = new MemoryStream(excelFile);
           Workbook excelWorkbook = new Workbook(memoryStream, new LoadOptions(LoadFormat.Xlsx));
           Chart chart = excelWorkbook.Worksheets[1].Charts[0];
           ImageOrPrintOptions imgProps = new ImageOrPrintOptions();
           imgProps.ImageType = ImageType.Png;
           var stream = new MemoryStream();
           chart.ToImage(stream, imgProps);
           Bitmap chartImage = new Bitmap(stream);
           chartImage.Save("e:\\test2\\outchart1.png");
[2]
            byte[] excelFile = File.ReadAllBytes("e:\\test2\\Trend.xlsx"); 
            var memoryStream = new MemoryStream(excelFile);
            Workbook excelWorkbook = new Workbook(memoryStream, new LoadOptions(LoadFormat.Xlsx));
            Chart chart = excelWorkbook.Worksheets[0].Charts[0];
            ImageOrPrintOptions imgProps = new ImageOrPrintOptions();
            imgProps.ImageType = ImageType.Png;
            var stream = new MemoryStream();
            chart.ToImage(stream, imgProps);
            Bitmap chartImage = new Bitmap(stream);
            chartImage.Save("e:\\test2\\outchart2.png");

Please find attached the output image files for your reference.
files1.zip (26.1 KB)

@Amjad_Sahi,
Switching to v22.8 didn’t fix it.
Did you try to generate the image several times, because the issue do not appear every time?

@hvasilev,

Could you please share a standalone sample (runnable) code (same as above) to reproduce the issue on our end, we will check it soon.

I found the reason for the issue, and I’m attaching a zip file with a small project, so you can reproduce it.
ConsoleApp1.zip (16.1 KB)
The Chart.ToImage is using the last set StringFormat.Alignment to draw the labels.

It is strange that I can reproduce it in standalone project only with Aspose.Cells 22.1, but in our application the issue persists even with version 23.1.1.
The workaround is to set the alignment and draw or measure string right before ToImage():

StringFormat textFormat = StringFormat.GenericTypographic;
textFormat.Alignment = StringAlignment.Near;
graphics.MeasureString("aaa", font, 1, textFormat);

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

  Issue ID(s): CELLSNET-52611

You can obtain Paid Support services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hi @hvasilev
Please change this code:

StringFormat textFormat = StringFormat.GenericTypographic;
textFormat.Alignment = StringAlignment.Near;

to:

StringFormat textFormat = new StringFormat(StringFormat.GenericTypographic);
textFormat.Alignment = StringAlignment.Near;

This is because your previous code changed the static var (StringFormat.GenericTypographic) properties, and all subsequent drawing operations (not just aspose cells) will be affected.

In order to reproduce this problem, I wrote a test program (not using aspose cells) , the drawing function was similarly affected, for reference.

StringFormatTest.zip (1.1 MB)

1 Like

Thank you for helping me fix the issue.
I updated my code and the reports are good now.

@hvasilev,

Good to know that your issue is sorted out now. Feel free to write us back if you have further queries or issue, we will be happy to assist you soon.