在 ASP.NET MVC 中使用 Microsoft 图表控件返回空白图像
使用来自 这篇文章,我创建了一个控制器操作来返回图表图像,如下所示(X 和 Y 值只是作为测试数据):
public FileContentResult HistoryChart()
{
Chart chart = new Chart();
string[] currencies = { "ZAR", "USD", "GBP", "JPY" };
foreach (string currency in currencies)
{
Series series = new Series(currency);
series.ChartType = SeriesChartType.FastLine;
for (int x = 0; x <= 30; x++)
series.Points.AddXY(x, (x * 5));
chart.Series.Add(series);
}
using (MemoryStream ms = new MemoryStream())
{
chart.SaveImage(ms, ChartImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
return File(ms.ToArray(), "image/png", "mychart.png");
}
}
问题是,控制器返回的图像是空白(尽管它确实返回图像)
我希望它是我遗漏的简单内容!任何意见将不胜感激,谢谢。
Using the answer Generating the Image from a Controller from this post, I created a controller action to return a chart image as seen below (the X and Y values are just there as test data):
public FileContentResult HistoryChart()
{
Chart chart = new Chart();
string[] currencies = { "ZAR", "USD", "GBP", "JPY" };
foreach (string currency in currencies)
{
Series series = new Series(currency);
series.ChartType = SeriesChartType.FastLine;
for (int x = 0; x <= 30; x++)
series.Points.AddXY(x, (x * 5));
chart.Series.Add(series);
}
using (MemoryStream ms = new MemoryStream())
{
chart.SaveImage(ms, ChartImageFormat.Png);
ms.Seek(0, SeekOrigin.Begin);
return File(ms.ToArray(), "image/png", "mychart.png");
}
}
The problem is, the image that the controller returns is blank (although it DOES return an image)
Im hoping its something simple that I have left out! Any input would be appreciated, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
希望这会有所帮助......
我遇到了同样的问题:
这都与颜色有关,在使用此博客中的另一个示例并从中推断出问题后,我向您添加了一些代码 - 所以“谢谢”大家....
此外,您还需要确保您的控制器具有:
using System.Drawing;
使用 System.Web.UI.WebControls;
为大家干杯...
JK。
Hope this helps.....
I've had the same problem:
It's all to do with colors, I added some code to yours after having used another example from this blog and deduced the issue from that - so 'Thanks' to everyone....
Also, you will need to ensure that your controller has:
using System.Drawing;
using System.Web.UI.WebControls;
Cheers to all...
JK.
嗨,我遇到了同样的问题,这是因为我在创建它的不同时间保存图像。渲染时内部状态丢失。
在保存图像之前再次测试生成图表。
对不起我的英语。
Hi I had the same problem and it was because I was saving the image at a different time that had created it. When rendering the internal state loses.
Test generating the chart again before save image.
Sorry for my english.