Dynamic graphs using C# (To be saved as a PNG Format)


Staff member
I have to generate dynamic graphs on the fly, save it as a PNG image format (Transparent background) to be embedded on to a PDF.

The problem is, the graph will not be displayed on a control in the page (without a chart control in the WebForm) and should be generated from an underlying information and should be embedded while creating the PDF.

The libraries for the PDFs and PDF generation is all in place.

I would like to know how to generate a dynamic graph (Bar graph, Pie Graph) 3D or Simple graph without a chart control in place and be able to save it as an image with transparent background (.PNG).

The reason being, the PDF template has numerous layer shading, hence the background around the graph image should be transparent, something similar to the below image.

<a href=" " rel="nofollow noreferrer"><img src=" " alt="Possible Graph"></a>

I have tried generating a dynamic bar graph similar to the example in the below link but the Image doesn't look promising

<a href="https://2leggedspider.wordpress.com/2004/11/21/generating-a-dynamic-bar-graph-using-aspnet-and-c/" rel="nofollow noreferrer">https://2leggedspider.wordpress.com/2004/11/21/generating-a-dynamic-bar-graph-using-aspnet-and-c/</a>

<a href=" " rel="nofollow noreferrer"><img src=" " alt="Graph implemented"></a>

Any help regarding the functionality is deeply appreciated. Thank you in advance.


<h2>Update: Wednesday 16th March 2016</h2>

With TaW's answer and updates I have been able to draw the required graph and below is the update with answer to the same question I asked

Add references to

Add below namespaces

using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

And below method would give you the Pie Chart with smooth edges as shown in the picture

<a href=" " rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GcTVq.png" alt="Pie Chart with Smooth Edges"></a>

void Create3DPieChart()
            /* Utilize the Chart class available in System.Windows.Forms.DataVisualization.Charting */
            Chart chart = new Chart();
            /* Add a chart area to the chart */
            ChartArea CA = chart.ChartAreas.Add("A1");
            /* Add data series to the chart and specify the type of Series */
            Series S1 = chart.Series.Add("S1");
            S1.ChartType = SeriesChartType.Pie;

            /* Assign points for the Series */
            S1.Points.AddXY(1, 17);
            S1.Points.AddXY(2, 27);
            S1.Points.AddXY(3, 7);
            S1.Points.AddXY(4, 49);

            /* Set chart color and other settings as required */
            chart.BackColor = Color.Transparent;
            CA.BackColor = chart.BackColor;
            CA.Area3DStyle.Enable3D = true;
            S1.Points[2]["Exploded"] = "true";

            /*Assign AntiAliasing to Graphics style for smooth edges*/
            chart.AntiAliasing = AntiAliasingStyles.Graphics;

            /* Set the image path and save the image as PNG format*/
            string imageNameAndPath = string.Concat(Application.StartupPath.Remove(Application.StartupPath.IndexOf("\\bin\\Debug")), 
                                        "/TempImages/Image", DateTime.Now.ToString("ddMMyyyyhhmmss") + ".png");
            chart.SaveImage(imageNameAndPath, ChartImageFormat.Png);

TaW. Please correct me if I'm wrong.