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

Free Support Forum - aspose.com

How to convert an excel sheet range to image in Apsose.cells c# where range has multiple charts

Hi,
I have an excel sheet having multiple charts in a range , I need to convert entire range to image where all charts should successfully copied to image .When I tried using following code charts are missing or partially available in image file .Please let me know if any solution in Aspose.cells available
My sample code -
ImageOrPrintOptions opts = new ImageOrPrintOptions();
opts.ImageType = ImageType.Png;
opts.OnePagePerSheet = true;
opts.HorizontalResolution = 200;
opts.VerticalResolution = 200;
opts.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
var render = new SheetRender(worksheet, opts);
using var stream = new MemoryStream();

        render.ToImage(0, stream);

        var imageContent = Convert.ToBase64String(stream.ToArray());
		//saving imageContent to file

@rasmi.mishra,

Please provide your template Excel file (you may zip prior attaching) so we could evaluate your issue on our end.

Hi Amjad,
Please find sample code and files
ReadWriteChart-Aspose.zip (33.2 KB)
Thanks

@rasmi.mishra,

Thanks for the sample file.

Which version of Aspose.Cells for .NET APIs you are using? I tested your scenario/case using the following sample code using your file with latest version/fix: Aspose.Cells for .NET v22.12 (Releases | NuGet), it works fine and the output image is fine tuned.
e.g.
Sample code:

var workbook = new Aspose.Cells.Workbook("e:\\test2\\SourceChart.xlsx");
WorksheetCollection worksheets = workbook.Worksheets;
var worksheet = worksheets[0];
workbook.Save("e:\\test2\\out1.pdf");

int resolution = 300;
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.OnePagePerSheet = true;
options.ImageType = ImageType.Png;
options.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
options.HorizontalResolution = resolution;
options.VerticalResolution = resolution;
            
var render = new SheetRender(worksheet, options);
var stream = new MemoryStream();
render.ToImage(0, stream);
var imageContent = Convert.ToBase64String(stream.ToArray());
Byte[] byteArray = Convert.FromBase64String(imageContent);
File.WriteAllBytes("e:\\test2\\out1.png", byteArray); 

out1.pdf (15.9 KB)
out1.png (46.7 KB)

Hi @Amjad_Sahi
When I am trying to read charts by excel range in Aspose , I could see only partial result in image . All charts are not coming to image .Please try with attached excel wher i have provided complete charts I am trying by range . Range name is ShapeRange1ReadChartByRange.zip (34.0 KB)
Let me know the code in Aspose to read given charts will help , thanks

@rasmi.mishra

Thanks for the new file.

I tested it with your new file with latest version using the exact code which I shared in my previous reply, it works fine and the output image is fine tuned. Please find the output image for your reference.
out1.png (342.5 KB)

Do you use some different code? If so, please paste your sample code (runnable) same as I shared the code segment previously. We will check your issue soon.

Hi @Amjad_Sahi
Please share me the code you have used to get charts via excel range reference
Thanks

@rasmi.mishra,

I simply used the following sample code to render the sheet (containing charts) to image:
e.g.
Sample code:

var workbook = new Aspose.Cells.Workbook("e:\\test2\\ReadChartByRange.xlsx");
WorksheetCollection worksheets = workbook.Worksheets;
var worksheet = worksheets[0];
workbook.Save("e:\\test2\\out1.pdf");

int resolution = 300;
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.OnePagePerSheet = true;
options.ImageType = ImageType.Png;
options.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
options.HorizontalResolution = resolution;
options.VerticalResolution = resolution;
            
var render = new SheetRender(worksheet, options);
var stream = new MemoryStream();
render.ToImage(0, stream);
var imageContent = Convert.ToBase64String(stream.ToArray());

Byte[] byteArray = Convert.FromBase64String(imageContent);
File.WriteAllBytes("e:\\test2\\out1.png", byteArray); 

the output image is already attached to previous post.

Do you use some different code? If so, please paste your sample code (runnable) same as I shared the code segment (above) . We will check your sample code.

Thanks @Amjad_Sahi,the issue was reading a specific name range chart before exporting as image , this has been fixed

@rasmi.mishra,

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