C# 具有背景栅格的绘曲线类(一)

    本文实现了用于绘制曲线的通用类,类实现对背景栅格的绘制,并将由外部传入到先进先出栈中的数据作为
曲线的关键点,返回一个位图的对象用于显示曲线。 
using System;

using System.Collections.Generic;

using System.Collections;

using System.Text;

using System.Drawing;



namespace PID_Control

{

    class Curve

    {

        public  Queue KeyDots=new Queue(); //曲线关键点

        private Graphics objGraphic;//提供绘制的方法

        public  Bitmap objBitmap;//位图对象



        private Point CurrentPoint = new Point(0, 0); //当前点

        private Point LastPoint = new Point(50, 635); //最后的点



        private int m_Width = 900;// 图像宽度

        private int m_Height = 650;//图像高度

        private int m_XGrid = 50;//栅格X长度

        private int m_YGrid = 50;//栅格Y长度



        private Color m_BorderColor = Color.Blue; //边框颜色

        private Color m_BgColor = Color.Black;//背景颜色 

        private Color m_GridColor = Color.Gray;//栅格颜色



        public int Width

        {

            set { m_Width =value; }

            get { return m_Width; }

        }

        public int Height

        {

            set { m_Height = value; }

            get { return m_Height; }

        }

        public Color BorderColor

        {

            set { m_BorderColor = value; }

            get { return m_BorderColor; }

        }

        public Color BgColor

        {

            set { m_BgColor = value; }

            get { return m_BgColor; }

        }

        public Color GridColor

        {

            set { m_GridColor = value; }

            get { return m_GridColor; }

        }

        public int XGrid

        {

            set { m_XGrid = value; }

            get { return m_XGrid; }

        }

        public int YGrid

        {

            set { m_YGrid = value; }

            get { return m_YGrid; }

        }



        public Curve()

        {

            InitializeGraph();

            DrawContent();

        }

        public void InitializeGraph()

        {

            //创建指定高度和宽度的位图对象

            objBitmap = new Bitmap(Width, Height);

            //从位图对象中创建Graphics对象

            objGraphic = Graphics.FromImage(objBitmap);

            //创建指定颜色的矩形边框

            objGraphic.DrawRectangle(new Pen(BorderColor, 1),0,0, Width, Height);

            objGraphic.FillRectangle(new SolidBrush(BgColor),1,1, Width, Height);

            //画X方向栅格并标注

            int t = 0;

            for (int i = Height-15; i >= 0; i -= YGrid)

            {

                objGraphic.DrawLine(new Pen(GridColor, 1), 50, i, Width, i);

                objGraphic.DrawString(System.Convert.ToString(t*5000), new Font("宋体", 10), new SolidBrush(Color.White), 0, i);

                t++;

            }

            //画Y方向栅格并标注

            t = 0;

            for (int i = 50; i < Width; i += XGrid)

            {

                objGraphic.DrawLine(new Pen(GridColor, 1), i, 0, i, Height-15);

                objGraphic.DrawString(System.Convert.ToString(t), new Font("宋体", 10), new SolidBrush(Color.White), i, Height-13);

                t++;

            }

        }

        public void DrawContent()

        {

            while (KeyDots.Count > 0)

            {

                //设置新点坐标

                CurrentPoint.X = LastPoint.X + 2; 

                CurrentPoint.Y = Height-System.Convert.ToInt16(KeyDots.Dequeue())-15;

                objGraphic.DrawLine(new Pen(Color.Red, 2),LastPoint,CurrentPoint);

                //重置最后点坐标

                LastPoint = CurrentPoint;

                if (LastPoint.X > Width)

                {

                    InitializeGraph();

                    LastPoint.X = 50;

                }

            }

        }

    }

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值