在 ASP.NET MVC 中使用 Microsoft 图表控件返回空白图像

发布于 2024-08-30 13:02:19 字数 1004 浏览 4 评论 0原文

使用来自 这篇文章,我创建了一个控制器操作来返回图表图像,如下所示(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栖竹 2024-09-06 13:02:19

希望这会有所帮助......

我遇到了同样的问题:

这都与颜色有关,在使用此博客中的另一个示例并从中推断出问题后,我向您添加了一些代码 - 所以“谢谢”大家....

    public FileContentResult HistoryChart()
    {
        Chart chart = new Chart();
        **chart.BackColor = Color.Transparent;**

        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);
        }

        **ChartArea ca1 = new ChartArea("ca1");
        ca1.BackColor = Color.Cyan;
        chart.ChartAreas.Add(ca1);**

        using (MemoryStream ms = new MemoryStream())
        {
            chart.SaveImage(ms, ChartImageFormat.Png);
            ms.Seek(0, SeekOrigin.Begin);

            return File(ms.ToArray(), "image/png", "mychart.png");
        }
    } 

此外,您还需要确保您的控制器具有:

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....

    public FileContentResult HistoryChart()
    {
        Chart chart = new Chart();
        **chart.BackColor = Color.Transparent;**

        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);
        }

        **ChartArea ca1 = new ChartArea("ca1");
        ca1.BackColor = Color.Cyan;
        chart.ChartAreas.Add(ca1);**

        using (MemoryStream ms = new MemoryStream())
        {
            chart.SaveImage(ms, ChartImageFormat.Png);
            ms.Seek(0, SeekOrigin.Begin);

            return File(ms.ToArray(), "image/png", "mychart.png");
        }
    } 

Also, you will need to ensure that your controller has:

using System.Drawing;
using System.Web.UI.WebControls;

Cheers to all...

JK.

江南烟雨〆相思醉 2024-09-06 13:02:19

嗨,我遇到了同样的问题,这是因为我在创建它的不同时间保存图像。渲染时内部状态丢失。
在保存图像之前再次测试生成图表。
对不起我的英语。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文