Let's create a simple ASP .NET application that shows a web page with a chart image. Please note that although we choose C# as the application language here, both C# and VB .NET versions of this demo are included in the Aspose.Chart msi installer.
The Chart class provided by Aspose.Chart can create an image of a chart, but it cannot render it to a web page automatically. Our task is to send the image to the browser so it appears on a web page. We will do this by adding an Image control to a page and setting its ImageUrl property to the URL of another page that will generate the chart image. When the browser needs to download the image, it will refer to the generator page and the Page_Load event handler will create a chart and save the chart image into the response stream.
1. Open VS .NET.
2. Create new ASP .NET project: File->New->Project.
3. Add an image control to a web form in the project.
4. Set the ImageUrl property of the control that specifies the URL where the browser should obtain the image from (note you can simply set it in the Properties window):
[C#]
public class Main : Page
{
protected Image ChartImage;
private void Page_Load(object sender, System.EventArgs e)
{
//The image of the chart will be generated by the ChartImage.aspx page,
//so specify it as a source.
ChartImage.ImageUrl = "ChartImage.aspx";
}
...
}
[VB.NET]
Public Class Main
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'The image of the chart will be generated by the ChartImage.aspx page,
'so specify it as a source.
ChartImage.ImageUrl = "ChartImage.aspx"
End Sub
...
End Class
5. Add a Web form: Project->Add Web Form ChartImage.aspx.
6. Add a reference to Aspose.Chart.dll: Project->Add Reference.
7. Implement the code in the Page_Load event handler of the ChartImage.aspx form that creates a chart image and saves it to the output stream:
[C#]
/// <summary>
/// This page generates a simple chart image and returns it in the HTTP response.
/// </summary>
public class ChartImage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
//Create a sample chart.
Chart c = new Chart();
c.Height = 300;
c.Width = 400;
Series s = new Series();
s.ChartType = ChartType.Line;
s.DataPoints.Add(new DataPoint(1, 1));
s.DataPoints.Add(new DataPoint(2, 2));
s.DataPoints.Add(new DataPoint(3, 3));
c.SeriesCollection.Add(s);
//Save the chart image into a memory stream.
MemoryStream ms = new MemoryStream();
c.Save(ms, ImageFormat.Png);
//Save the chart image from the memory stream to the response.
Response.Clear();
Response.ContentType = "image/png";
Response.OutputStream.Write(ms.ToArray(), 0, (int) ms.Length);
}
...
}
[VB.NET]
Imports System.IO
Imports System.Drawing.Imaging
Imports Aspose.Chart
' <summary>
' This page generates a simple chart image and returns it in the HTTP response.
' </summary>
Public Class ChartImage
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Create a sample chart.
Dim c As Chart = New Chart
c.Height = 300
c.Width = 400
Dim s As Series = New Series
s.ChartType = ChartType.Line
s.DataPoints.Add(New DataPoint(1, 1))
s.DataPoints.Add(New DataPoint(2, 2))
s.DataPoints.Add(New DataPoint(3, 3))
c.SeriesCollection.Add(s)
'Save the chart image into a memory stream.
Dim ms As MemoryStream = New MemoryStream
c.Save(ms, ImageFormat.Png)
'Save the chart image from the memory stream to the response.
Response.Clear()
Response.ContentType = "image/png"
Response.OutputStream.Write(ms.ToArray(), 0, ms.Length)
End Sub
End Class
8. Start the application: Debug->Start.