Image / Png Content Response Type

I am trying to create an aspx page returning Image/Png chartDirector from chartDirector

Here is what I have in my VB:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim mychart As XYChart = New XYChart(700, 170) Dim values As Double() = {25, 18, 15, 12, 8, 30, 35} Dim labels As String() = {"Labor", "Licenses", "Taxes", "Legal", "Insurance", "Facilities", "Production"} mychart.setPlotArea(30, 20, 200, 200) mychart.addBarLayer(values) Response.ContentType = "image/png" Response.BinaryWrite(mychart.makeChart2(Chart.PNG)) Response.Close() End Sub 

When I launch this page, I get this output:

I got this idea from the following asp code

  <%@ language="vbscript" %> <% Set cd = CreateObject("ChartDirector.API") 'The data for the bar chart data = Array(85, 156, 179.5, 211, 123) 'The labels for the bar chart labels = Array("Mon", "Tue", "Wed", "Thu", "Fri") 'First, create a XYChart of size 250 pixels x 250 pixels Set c = cd.XYChart(250, 250) 'Set the plotarea rectangle to start at (30, 20) and of 322 '200 pixels in width and 200 in height Call c.setPlotArea(30, 20, 200, 200) 'Add a bar chart layer using the supplied data Call c.addBarLayer(data) 'Set the x-axis labels using the supplied labels Call c.xAxis().setLabels(labels) 'output the chart Response.contenttype = "image/png" Response.binarywrite c.makeChart2(cd.PNG) Response.end %> 

and he used the img src associated with this page to render the image

QUESTION: how can I execute the same implementation in aspx ?

Notice that I know little about .Net. I just started.

+4
source share
2 answers

Use Response.End instead of Response.Close .

The response is buffered, so if you close it, the browser will not get what is in the buffer, unless you clear the buffer before closing the stream.

+1
source

This is the case when you can use a custom .ashx HttpHandler rather than a classic .aspx . Here is a good introduction to using them.

You basically inherit the IHttpHandler interface, which defines the ProcessRequest method. Unfortunately, I only know C #.

 public class CustomImageHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { // here you'll use context.Response to set the appropriate // content and http headers context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.ContentType = "image/png"; byte[] responseImage = GenerateImage(); context.Response.BinaryWrite(responseImage); context.Response.Flush(); } } 
+3
source

All Articles