C#中System.Drawing制作折线图

核心代码:

    /// 
    /// 创建折线图
    /// 
    /// 所需折线图的宽度
    /// 所需折线图的高度
    /// 所需画出的点
    /// 折线图颜色
    /// 
    public static Texture2D LineChart3d(ref Texture2D lc, int width,int height,PointF[] points, System.Drawing.Color color)
    {
        if (lc == null)
        {
            lc = new Texture2D(width, height);
        }

        Bitmap bitmap = new Bitmap(width, height);
        System.Drawing.Graphics ccd = System.Drawing.Graphics.FromImage(bitmap);
        ccd.SmoothingMode = SmoothingMode.AntiAlias;
        ccd.Clear(System.Drawing.Color.Transparent);

        //画曲线图
        System.Drawing.Pen pen = new Pen(color);
        pen.Width = width / 100;

        //画点
        //float pWidth = width / 30;
        //float pHeight = width / 30;
        //for (int i = 0; i != points.Length; ++i)
        //{
        //    points[i].X *= width;
        //    points[i].Y = (1 - points[i].Y) * height;
        //    ccd.FillEllipse(new SolidBrush(color), new RectangleF(points[i].X - pWidth / 2.0f, points[i].Y - pHeight / 2.0f, pWidth, pHeight));
        //}
        for (int i = 0; i < points.Length; ++i)
        {
            points[i] = new PointF(points[i].X * width, points[i].Y * height);
        }
        ccd.DrawLines(pen, points);

        //for (int i = 0; i != points.Length - 1; ++i)
        //{
        //    ccd.DrawLine(pen, points[i], points[i + 1]);
        //}

        byte[] byteArray = new byte[0];
        using (MemoryStream stream = new MemoryStream())
        {
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            stream.Close();

            byteArray = stream.ToArray();
        }

        lc.LoadImage(byteArray);
        return lc;
    }


你可能感兴趣的:(unity,C#)