渲染线图以附加到电子邮件

时间:2014-08-13 15:06:49

标签: c# graph

我正在尝试渲染折线图,作为我可以附加到电子邮件的某种图像。我的所有搜索只会显示有助于渲染控件的库。我的应用程序没有UI或Web组件,这些组件不适用于我找到的库。

有没有人知道任何可以帮助我解决这个问题的lib / tutorial。我的线图根本不需要花哨,我只需要展示一些不同颜色的系列。我不需要任何标签,我甚至可以在没有传奇的情况下离开。

1 个答案:

答案 0 :(得分:2)

以下是使用WinForm Chart控件创建位图的控制台应用程序示例。它将它保存到磁盘,但您也可以将它附加到您的邮件..

首先在项目中包含对这些程序集的引用:System.Drawing, System.Windows.Forms and System.Windows.Forms.DataVisualization

然后使用以下内容添加这些:

using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.DataVisualization.Charting;

现在您可以使用Chart控件:

class Program
{
    static void Main(string[] args)
    {
        Bitmap bmp = getChartImage();
        bmp.Save("yourpathandfilename.png");
        bmp.Dispose();
    }

    static Bitmap getChartImage()
    {
        Chart chart = new Chart();
        ChartArea chartArea = new ChartArea();
        chart.ChartAreas.Add(chartArea);
        Legend legend = new Legend(); // optional
        chart.Legends.Add(legend);    // optional
        chart.ClientSize = new Size(800, 500);

        // a few test data 
        for (int s = 0; s < 5; s++)
        {
            Series series = chart.Series.Add(s.ToString("series 0"));
            series.ChartType = SeriesChartType.Line;
        }
        for (int s = 0; s < 5; s++)
            for (int p = 0; p < 50; p++)
                chart.Series[s].Points.AddXY(p, s * p);

        Bitmap bmp = new Bitmap(chart.ClientSize.Width, chart.ClientSize.Height);
        chart.DrawToBitmap(bmp, chart.ClientRectangle);

        return bmp;
    }
}

我建议您创建一个Winforms应用程序来使用Control提供的许多选项。然后它们都可以应用到控制台版本..对于你可以在设计师中做的很多事情也是如此!