|
本文将讲述利用C#语言绘制一个实时曲线图,可用来显示CPU的使用频率以及播放声音视频时实时显示当前的声频等等!
为了操作和应付变化,所以将绘制曲线图的功能单独封装成一个类,里面的数据完全是模拟的,在横向坐标上每个像素间隔用一个点来控制(实际中可能会加大这个距离),横向是个随机生成的数(实际开发中这应该来自我们的实时数据按比率计算得来的),显示窗体中用到了一个线程来定时绘制实时曲线。
实际代码如下:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Imaging;
-
- namespace RealtimeCurve
- {
-
-
-
-
-
- public class RealTimeImageMaker
- {
- private int width;
- private int height;
- private Point[] pointList;
- private Random random = new Random();
- private Bitmap currentImage;
- private Color backColor;
- private Color foreColor;
-
-
-
- public int Height
- {
- get { return height; }
- set { height = value; }
- }
-
-
-
-
- public int Width
- {
- get { return width; }
- set { width = value; }
- }
-
-
-
-
-
- public RealTimeImageMaker(int width, int height):this(width,height,Color.Gray,Color.Blue)
- {
-
- }
-
-
-
-
-
-
-
- public RealTimeImageMaker(int width, int height, Color backColor, Color foreColor)
- {
- this.width = width;
- this.height = height;
- this.backColor = backColor;
- this.foreColor = foreColor;
- pointList = new Point[width];
- Point tempPoint;
-
- for (int i = 0; i < width; i++)
- {
-
- tempPoint = new Point();
-
- tempPoint.X = i;
-
- tempPoint.Y = random.Next() % height;
- pointList[i] = tempPoint;
- }
- }
-
-
-
-
- public Image GetCurrentCurve()
- {
-
- currentImage = new Bitmap(width, height);
- Point p;
-
-
- for (int i = 0; i < width-1; i++)
- {
- p = pointList[i + 1];
- pointList[i] = new Point(p.X-1,p.Y);
- }
- Point tempPoint = new Point();
-
- tempPoint.X = width;
-
- tempPoint.Y = random.Next(DateTime.Now.Millisecond) % height;
-
- pointList[width-1]=tempPoint;
- Graphics g = Graphics.FromImage(currentImage);
- g.Clear(backColor);
-
- g.DrawLines(new Pen(foreColor), pointList);
- g.Dispose();
- return currentImage;
- }
- }
- }
共2页: 上一页 1 [2] 下一页
|