内容响应类型图像/ Png

时间:2012-08-03 12:27:48

标签: c# asp.net .net vb.net visual-studio-2010

我正在尝试创建一个从Image/Png

返回chartDirector的aspx页面

到目前为止,这是我在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 
   %> 

并使用链接到此页面的img src来呈现图片

问题是如何在aspx中执行相同的实现的?

注意我对.Net刚开始不太了解。

2 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:2)

使用Response.End代替Response.Close

响应是缓冲的,所以如果你关闭它,浏览器就不会得到缓冲区中的内容,除非你在关闭流之前刷新缓冲区。