.NET : 如何利用GDI+绘制折线图

这是今天课堂上讲的一个范例小程序。 其实很多图表控件大多也是这样画出来的。           
            //如何从零开始构造一个图片
            Bitmap b = new Bitmap(600, 400);
            Graphics bg = Graphics.FromImage(b);
            //背景颜色先清除掉
            bg.Clear(Color.LightGray);

            //先画横轴
            bg.DrawLine(
                new Pen(Color.Black),
                new Point(10, 380),
                new Point(580, 380));

            bg.DrawLine(
                new Pen(Color.Black),
                new Point(570, 370),
                new Point(580, 380));

            bg.DrawLine(
                new Pen(Color.Black),
                new Point(570, 390),
                new Point(580, 380));


            //再画纵轴
            bg.DrawLine(
                new Pen(Color.Black),
                new Point(20, 390),
                new Point(20, 20));

            bg.DrawLine(
                new Pen(Color.Black),
                new Point(10, 30),
                new Point(20, 20));
            bg.DrawLine(
                new Pen(Color.Black),
                new Point(30, 30),
                new Point(20, 20));


            //画我们那条趋势线
            List<Point> points = new List<Point>()
            {
                new Point(20,380),
                new Point(40,365),
                new Point(55,350),
                new Point(100,300),
                new Point(200,120),
                new Point(570,30)};

            bg.DrawLines(
                new Pen(Color.Red),
                points.ToArray());


            foreach (var item in points)
            {
                item.Offset(-10, -10);

                bg.FillEllipse(
                    new SolidBrush(Color.Yellow),
                    new Rectangle(item,new Size(20,20)));


                item.Offset(5, 5);
                bg.DrawString(
                    item.Y.ToString(),
                    new Font("Arial", 6),
                    new SolidBrush(Color.Blue),
                    item);

            }

            bg.Dispose();

            b.Save("demo.bmp");

 

 

demo
posted @ 2009-12-16 20:15  陈希章  阅读(1593)  评论(0编辑  收藏  举报