日期:2014-05-16  浏览次数:20881 次

c#画曲线问题
随机产生数据,每产生一个点就调用画曲线的函数,函数如下,每次只刷新一小块,为什么曲线显示时偶尔曲线会断
private void DrawWave(int data)
        {
            Graphics graph = picture.CreateGraphics();
            Pen pen = new Pen(Color.Black, 1);
            SolidBrush solidBrush = new SolidBrush(Color.White);
            graph.FillRectangle(solidBrush, x + 1, 0, 20, this.Height);
            graph.DrawLine(pen, x, picture.Height / 2 - oldy, x + 1, picture.Height / 2 - data);
            oldy= data;
            x++;
            if (x >= picture.Width)
                x = 0;
        }
是不是窗体自动重绘了背景?怎么解决
------解决方案--------------------
FillRectangle表示填充,会覆盖。
------解决方案--------------------
你的矩形区域可能覆盖了已经存在的曲线
------解决方案--------------------
终于晓得你的意思了

问题出在这里
 graph.FillRectangle(solidBrush, x + 1, 0, 20, this.Height); // 这里, 你每次都会覆盖20个单位

graph.DrawLine(pen, x, picture.Height / 2 - oldy1, x + 1, picture.Height / 2 - data); // 画线只画了(x+1) - x = 1个单位

改成 graph.FillRectangle(solidBrush, x + 1, 0, 1, this.Height);

另外一个问题 就是picture.Height / 2 - data 会有负数 不会画到图上 也会产生空白