Improper Charts images are created when using Workbook created using saved bytes data?

Hi,

I am facing an issue when generating chart images, when am using the workbook which is created using bytes[] of its previously saved version.
But the same will work fine, if i create the workbook object directly from my file system.

Below is the code which you can directly try on your end and see it outputs two images one created using file which is present in your file system, Other which is created using bytes[] data of its previously saved version.
Note: Please change the file path and image saving paths as per your file system.

string filePath = @"Your System Path\ExcelLinksFile.xlsx";//change the path as per the file location in your machine.
            string sheetName = "Sheet_ColumnChart";// 2nd sheet
            string chartName = "Chart3";

            #region 1st Approach - Saving Chart from Excel File present in FileSystem.
            //1.  Saving Chart from Excel File present in FileSystem.
            Aspose.Cells.Workbook workbookFromFileSystem1 = new Aspose.Cells.Workbook(filePath);
            //Load workbook sheet, chart is not getting displayed properly.
            Aspose.Cells.Worksheet sourceWorksheet1 = workbookFromFileSystem1.Worksheets[sheetName];

            // Apply worksheet settings
            sourceWorksheet1.PageSetup.LeftMargin =
           sourceWorksheet1.PageSetup.RightMargin =
           sourceWorksheet1.PageSetup.TopMargin =
           sourceWorksheet1.PageSetup.BottomMargin = 0;
            sourceWorksheet1.AutoFitColumns();

            Aspose.Cells.Charts.Chart sourceChart1 = sourceWorksheet1.Charts[chartName];
            sourceChart1.Calculate();
            try
            {
                // set imge or print options.
                Aspose.Cells.Rendering.ImageOrPrintOptions imageOrPrintOptions1 = new Aspose.Cells.Rendering.ImageOrPrintOptions();
                imageOrPrintOptions1.OnePagePerSheet = true;
                imageOrPrintOptions1.ImageType = Aspose.Cells.Drawing.ImageType.Png;
                imageOrPrintOptions1.HorizontalResolution = 200;
                imageOrPrintOptions1.VerticalResolution = 200;
                imageOrPrintOptions1.IsCellAutoFit = true;

                System.Drawing.Image imageToReturn1 = sourceChart1.ToImage(imageOrPrintOptions1);
                imageToReturn1.Save(@"Your System Path\ChartFromFileSystem.png");// Change the save path as per your need.
            }
            catch
            {
                // if invalid range is given, then aspose fails to convert the range into image.
            }

            #endregion

            #region 2nd Approach - Saving Chart from Excel File Created Using bytes[].
            //2.  Saving Chart from Excel File Created Using bytes[].
            Aspose.Cells.Workbook workbookFromFileSystem = new Aspose.Cells.Workbook(filePath);
            
            // Save workbook into bytes
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            workbookFromFileSystem.Save(ms, Aspose.Cells.SaveFormat.Excel97To2003);
            ms.Position = 0;
            byte[] bytesWorkbook = new byte[ms.Length];
            ms.Read(bytesWorkbook, 0, bytesWorkbook.Length);

            // Create workbook from bytes
            ms = new System.IO.MemoryStream();
            ms.Write(bytesWorkbook, 0, bytesWorkbook.Length);
            Aspose.Cells.Workbook workbookFromBytes = new Aspose.Cells.Workbook(ms);
            ms.Dispose();

            //Load workbook sheet, chart is not getting displayed properly.
            Aspose.Cells.Worksheet sourceWorksheet = workbookFromBytes.Worksheets[sheetName];

            // Apply worksheet settings
            sourceWorksheet.PageSetup.LeftMargin =
           sourceWorksheet.PageSetup.RightMargin =
           sourceWorksheet.PageSetup.TopMargin =
           sourceWorksheet.PageSetup.BottomMargin = 0;
            sourceWorksheet.AutoFitColumns();

            Aspose.Cells.Charts.Chart sourceChart = sourceWorksheet.Charts[chartName];
            sourceChart.Calculate();
            try
            {
                // set imge or print options.
                Aspose.Cells.Rendering.ImageOrPrintOptions imageOrPrintOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions();
                imageOrPrintOptions.OnePagePerSheet = true;
                imageOrPrintOptions.ImageType = Aspose.Cells.Drawing.ImageType.Png;
                imageOrPrintOptions.HorizontalResolution = 200;
                imageOrPrintOptions.VerticalResolution = 200;
                imageOrPrintOptions.IsCellAutoFit = true;

                System.Drawing.Image imageToReturn = sourceChart.ToImage(imageOrPrintOptions);
                imageToReturn.Save(@"Your System Path\ChartFromBytes.png");// Change the save path as per your need.               
            }
            catch
            {
                // if invalid range is given, then aspose fails to convert the range into image.
            }

            #endregion

Please review and let us know the issue reason and fix as soon as you can.
I have attached the files here (files 2.zip (74.5 KB)
) that am using and also the images that i generated using both of the approaches. You can see the difference there too.

Thanks,
Prathap

@PrathapSV,

Thanks for the template file and sample code.

Please try our latest version/fix: Aspose.Cells for .NET v19.4.5 (attached)
Aspose.Cells19.4.5 For .Net2_AuthenticodeSigned.Zip (4.9 MB)
Aspose.Cells19.4.5 For .Net4.0.Zip (4.9 MB)

I have tested using your template file and following sample code (please notice the sample code (updated) a bit) with v19.4.5, it works fine and both images are ok and save:
e.g
Sample code:

string filePath = “e:\test2\ExcelLinksFile.xlsx”;//change the path as per the file location in your machine.
string sheetName = “Sheet_ColumnChart”;// 2nd sheet
string chartName = “Chart3”;

        #region 1st Approach - Saving Chart from Excel File present in FileSystem.
        //1.  Saving Chart from Excel File present in FileSystem.
        Aspose.Cells.Workbook workbookFromFileSystem1 = new Aspose.Cells.Workbook(filePath);
        //Load workbook sheet, chart is not getting displayed properly.
        Aspose.Cells.Worksheet sourceWorksheet1 = workbookFromFileSystem1.Worksheets[sheetName];

        // Apply worksheet settings
        sourceWorksheet1.PageSetup.LeftMargin =
       sourceWorksheet1.PageSetup.RightMargin =
       sourceWorksheet1.PageSetup.TopMargin =
       sourceWorksheet1.PageSetup.BottomMargin = 0;
        sourceWorksheet1.AutoFitColumns();

        Aspose.Cells.Charts.Chart sourceChart1 = sourceWorksheet1.Charts[chartName];
        sourceChart1.Calculate();
        try
        {
            // set imge or print options.
            Aspose.Cells.Rendering.ImageOrPrintOptions imageOrPrintOptions1 = new Aspose.Cells.Rendering.ImageOrPrintOptions();
            imageOrPrintOptions1.OnePagePerSheet = true;
            imageOrPrintOptions1.ImageType = Aspose.Cells.Drawing.ImageType.Png;
            imageOrPrintOptions1.HorizontalResolution = 200;
            imageOrPrintOptions1.VerticalResolution = 200;
            imageOrPrintOptions1.IsCellAutoFit = true;

            System.Drawing.Image imageToReturn1 = sourceChart1.ToImage(imageOrPrintOptions1);
            imageToReturn1.Save("e:\\test2\\ChartFromFileSystem1.png");// Change the save path as per your need.
        }
        catch
        {
            // if invalid range is given, then aspose fails to convert the range into image.
        }

        #endregion

        #region 2nd Approach - Saving Chart from Excel File Created Using bytes[].
        //2.  Saving Chart from Excel File Created Using bytes[].
        Aspose.Cells.Workbook workbookFromFileSystem = new Aspose.Cells.Workbook(filePath);

        // Save workbook into bytes
        System.IO.MemoryStream ms = new System.IO.MemoryStream();

workbookFromFileSystem.Save(ms, Aspose.Cells.SaveFormat.Xlsx);
ms.Position = 0;
byte[] bytesWorkbook = new byte[ms.Length];
ms.Read(bytesWorkbook, 0, bytesWorkbook.Length);

        // Create workbook from bytes
        ms = new System.IO.MemoryStream();
        ms.Write(bytesWorkbook, 0, bytesWorkbook.Length);
        Aspose.Cells.Workbook workbookFromBytes = new Aspose.Cells.Workbook(ms);
        ms.Dispose();

        //Load workbook sheet, chart is not getting displayed properly.
        Aspose.Cells.Worksheet sourceWorksheet = workbookFromBytes.Worksheets[sheetName];

        // Apply worksheet settings
        sourceWorksheet.PageSetup.LeftMargin =
       sourceWorksheet.PageSetup.RightMargin =
       sourceWorksheet.PageSetup.TopMargin =
       sourceWorksheet.PageSetup.BottomMargin = 0;
        sourceWorksheet.AutoFitColumns();

        Aspose.Cells.Charts.Chart sourceChart = sourceWorksheet.Charts[chartName];
        sourceChart.Calculate();
        try
        {
            // set imge or print options.
            Aspose.Cells.Rendering.ImageOrPrintOptions imageOrPrintOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions();
            imageOrPrintOptions.OnePagePerSheet = true;
            imageOrPrintOptions.ImageType = Aspose.Cells.Drawing.ImageType.Png;
            imageOrPrintOptions.HorizontalResolution = 200;
            imageOrPrintOptions.VerticalResolution = 200;
            imageOrPrintOptions.IsCellAutoFit = true;

            System.Drawing.Image imageToReturn = sourceChart.ToImage(imageOrPrintOptions);
            imageToReturn.Save("e:\\test2\\ChartFromBytes.png");// Change the save path as per your need.               
        }
        catch
        {
            // if invalid range is given, then aspose fails to convert the range into image.
        }

        #endregion

Hope, this helps a bit.