改进一、不需要每次构造一个Bitmap对象,而只是每次填充背景颜色重绘; 改进二、增加绘制文本的函数,可以在图上添加注释信息。 class CurveBMP { Queue<int> dataQueue = new Queue<int>(1); Bitmap curveBMP; Color bgColor,foreColor; int width, height; public CurveBMP(Color bgColor, Color foreColor, int width, int height) { this.bgColor = bgColor; this.foreColor = foreColor; this.width = width; this.height = height; curveBMP = new Bitmap(this.width, this.height); } public Bitmap plot_curve(int data) { Graphics g = Graphics.FromImage(curveBMP); GraphicsUnit unit = GraphicsUnit.Pixel; RectangleF bounds = curveBMP.GetBounds(ref unit); //以像素的方式获取位图的边界,以便使用画刷填充背景颜色 SolidBrush fill_brush = new SolidBrush(bgColor); //设置填充画刷的颜色为背景颜色 g.FillRectangle(fill_brush,bounds); dataQueue.Enqueue(data); if (dataQueue.Count < 2) { return curveBMP; } if (dataQueue.Count > this.width) { dataQueue.Dequeue(); } int i=0; Point[] pts = new Point[dataQueue.Count]; foreach (byte ele in dataQueue) { pts[i] = new Point(i, -5*ele+height / 2); i++; } Pen pen = new Pen(this.foreColor, 1); g.DrawLines(pen,pts); pen.Dispose(); g.Dispose(); return curveBMP; } public Bitmap draw_text(int x,int y,string text) { Graphics g = Graphics.FromImage(curveBMP); Pen pen = new Pen(this.foreColor, 1); g.DrawString(text,new Font(FontFamily.GenericSerif,8),Brushes.Snow,x,y+height/2); pen.Dispose(); g.Dispose(); return curveBMP; } }