C#中的折线图

时间:2017-05-23 05:37:28

标签: c# charts linegraph

我想创建一个折线图并保存为图像,以便我可以将图像附加到电子邮件中。 这是实现,但保存的图像没有图形。

public void CreateLineGraph(string savePath)
        {
            var chart1 = new Chart
            {
                Height = 300,
                Width = 500
            };
            var series = new Series("Export");
            IEnumerable<string> xValue = DaysInAWeek1();
            IEnumerable<int> yValue = RangeValue1();
            series.Points.DataBindXY(xValue, yValue);
            series.Points.AddXY(10,20);
            chart1.Series.Add(series);
            series.ChartType = SeriesChartType.Line;

            chart1.SaveImage(savePath, ChartImageFormat.Png);
        }
        private IEnumerable<string> DaysInAWeek1()
        {
            IEnumerable<string> m_oEnum = new string[] { "1", "2", "3" };
            return m_oEnum;
        }

        private IEnumerable<int> RangeValue1()
        {
            IEnumerable<int> m_oEnum = new int[] { 0, 5, 10 };
            return m_oEnum;
        }

保存图片: enter image description here

1 个答案:

答案 0 :(得分:0)

您可以从屏幕截取屏幕截图并保存! 并切割你想要的图片(包含图表的图片部分)。 我的代码是这样的:

    IDataObject data;
                Image bmap;
                //---copy the image to the Clipboard---
                SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0);
                //---retrieve the image from Clipboard and convert it
                // to the bitmap format---
                data = Clipboard.GetDataObject();

                if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
                {
                    bmap =
                        ((Image)(data.GetData(typeof(
                            System.Drawing.Bitmap))));

                    if (!Directory.Exists("Your Directory that u wanna to save pic on it!"))
                    {
                        Directory.CreateDirectory("Directory path");
                    }
                    bmap.Save("The Directory u wanna to save and the filename", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
不要忘记在表单上添加此api:

 [System.Runtime.InteropServices.DllImport(
        "user32", EntryPoint = "SendMessageA")]
    static extern int SendMessage(
    int hwnd, int Msg, int wParam,
    [MarshalAs(UnmanagedType.AsAny)] object lParam);