2012-08-03 74 views
4

我試圖創建一個aspx頁面,從一個chartDirector內容響應型圖像/ PNG

回到這裏Image/Png是我在我的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 

當我運行這個頁面我得到這個輸出:

我從下面的ASP代碼

<%@ 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 
    %> 

這個想法,並使用imgsrc鏈接到這個頁面來呈現圖像

問題是我該怎麼做aspx相同的實現?

注意事項我對.NET剛開始並不瞭解太多。

回答

1

使用Response.End而不是Response.Close

響應被緩衝,所以如果關閉它,瀏覽器將不會獲取緩衝區中的內容,除非在關閉流之前刷新緩衝區。

3

這種情況下,您可能想使用自定義.ashx HttpHandler而不是傳統的.aspx頁面。 Here's這是一個很好的介紹。

基本上,您將繼承IHttpHandler接口,該接口定義ProcessRequest方法。我不幸只知道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(); 
    } 
}