Articles in this section
Category / Section

How to create a chart and save as an image using ASP.NET Core Presentation library

2 mins read

Syncfusion Presentation is a .NET PowerPoint Library used to create, read, edit and convert PowerPoint files to PDF and images programmatically without Microsoft Office or interop dependencies. Using this library, you can create a chart and save as an image using C#.

Steps to create a chart and save as an image

  1. Create a new C# console application project.

Create .NET console application in Visual Studio

  1. Install the Syncfusion.PresentationRenderer.Net.Core NuGet package as a reference to your .NET applications from NuGet.org.

Add PresentationRenderer.Net.Core NuGet packages

  1. Include the following namespace in the Program.cs file.

C#

using Syncfusion.OfficeChart;
using Syncfusion.Presentation;
using Syncfusion.PresentationRenderer;
  1. Use the following code example to create a chart and save as an image using the PowerPoint library.

C#

//Creates a Presentation instance
IPresentation pptxDoc = Presentation.Create();
//Adds a blank slide to the Presentation
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Add a Pie chart
IPresentationChart pieChart = CreatePieChart(slide);
//Initialize the PresentationRenderer
pptxDoc.PresentationRenderer = new PresentationRenderer();
// Converts the chart to image.
Stream image = new FileStream("../../../ChartToImage.jpg", FileMode.Create, FileAccess.ReadWrite);
pptxDoc.PresentationRenderer.ConvertToImage(pieChart, image);
//Closes the presentation
pptxDoc.Close();
image.Close(); 
  1. Add the below helper methods to create a chart.

C#

IPresentationChart CreatePieChart(ISlide slide)
{
  //Adds chart to the slide with position and size
  IPresentationChart chart = slide.Charts.AddChart(100, 10, 500, 300);
  chart.ChartType = OfficeChartType.Pie;

  //Assign data
  chart.DataRange = chart.ChartData[2, 1, 6, 2];
  chart.IsSeriesInRows = false;

  //Set data to the chart- RowIndex, columnIndex, and data
  chart.ChartData.SetValue(1, 1, "Food");
  chart.ChartData.SetValue(2, 1, "Fruits");
  chart.ChartData.SetValue(3, 1, "Vegetables");
  chart.ChartData.SetValue(4, 1, "Dairy");
  chart.ChartData.SetValue(5, 1, "Protein");
  chart.ChartData.SetValue(6, 1, "Grains");
  chart.ChartData.SetValue(1, 2, "Percentage");
  chart.ChartData.SetValue(2, 2, 36);
  chart.ChartData.SetValue(3, 2, 14);
  chart.ChartData.SetValue(4, 2, 13);
  chart.ChartData.SetValue(5, 2, 28);
  chart.ChartData.SetValue(6, 2, 9);

  //Set Chart Title
  chart.ChartTitle = "Pie Chart";

  return chart;
}

A complete working sample of creating a chart and saving as an image using the PowerPoint library using C# can be downloaded from GitHub.

By executing the program, you will get the image as follows.

Output image generated

Take a moment to peruse the documentation, where you can find basic PowerPoint Presentation processing options along with features like, find and replace text, working with charts, animation, smart arts and many more in the PowerPoint Presentation, and most importantly PDF conversion with code examples.

Explore more about the rich set of Syncfusion PowerPoint Framework features.

Conclusion

I hope you enjoyed learning about how to create a chart and save as an image using ASP.NET Core Presentation library.

You can refer to our ASP.NET Core Presentation feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core Presentation example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied