Tom Bowen
Tom Bowen

Reputation: 8534

C# Chart to base64 PNG

In an MVC Web Application, I need to be able to create a Chart using data in C#, and convert this into a bas64 representation of a PNG, to pass to some XSLT and include in a PDF.

I have tried using: System.Web.Helpers.Chart - this works, but is limited, and not of great quality. I can't get it to look exactly as I like.

OxyPlot - Seemed a lot more promising, but I am unable to convert to PNG in a Web Application, Export is only possible in WPF or Win Forms.

HighCharts - No way to render to PNG, without using something else like PhantomJS.

Is there a Chart library I can use to achieve this?

Upvotes: 0

Views: 1385

Answers (1)

akashperfect
akashperfect

Reputation: 291

I have used this System.Web.UI.DataVisualization.Charting Link

You can add this System.Web.UI.DataVisualization as a reference in your project. And for converting the chart to base64 image you can just use this.

private string GetChartBase64Image(Chart chart)
{
    using (MemoryStream memStream = new MemoryStream())
    {
        chart.SaveImage(memStream, ChartImageFormat.Png);
        byte[] imageArray = memStream.ToArray();

        return Convert.ToBase64String(imageArray);
    }
}

In above the example, Class Chart and ChartImageFormat are defined in namespace System.Web.UI.DataVisualization.Charting

Upvotes: 1

Related Questions