C# 内容响应类型图像/PNG
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11795737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Content Response Type Image/Png
提问by Mina Gabriel
I'm trying to create a aspx page that return Image/Pngfrom a chartDirector
我正在尝试创建一个Image/Png从chartDirector
Here is what I have in my VB so far:
到目前为止,这是我在 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 run this page I get this output:
当我运行此页面时,我得到以下输出:
I got this idea from the following asp code
我从以下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
%>
and it used imgsrclinked to this page to render the image
并使用imgsrc链接到此页面来呈现图像
QUESTIONis how can I do the same implementation in aspx?
问题是我如何在 中执行相同的实现aspx?
NoticeI don't know much about .Net I just started.
请注意,我对 .Net 知之甚少,我刚刚开始。
采纳答案by Guffa
Use Response.Endinstead of Response.Close.
使用Response.End代替Response.Close。
The response is buffered, so if you close it the browser doesn't get what's in the buffer unless you flush the buffer before closing the stream.
响应是缓冲的,所以如果你关闭它,浏览器不会得到缓冲区中的内容,除非你在关闭流之前刷新缓冲区。
回答by Manu Letroll
That's a case where you might want to use a custom .ashxHttpHandler rather than a classic .aspxpage. Here'sa nice introduction to using these.
在这种情况下,您可能想要使用自定义.ashxHttpHandler 而不是经典.aspx页面。这是使用这些的很好的介绍。
Basically you'll inherit the IHttpHandlerinterface, which defines a ProcessRequestmethod. I unfortunately only know C#.
基本上你会继承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();
}
}


