public Bitmap mybitmap;
//用于双缓冲的位图,和画布等大
Random rm = new Random(); //随机数产生器
public void DrawLineS(Color color, float Xmax, float Ymax, PictureBox picbox, Point[] ptlist)
{
mybitmap = new Bitmap(picbox.Width, picbox.Height); //设定位图大小
Graphics doublebufferg = Graphics.FromImage(mybitmap); //从位图上获取“画布”
doublebufferg.Clear(Color.White); //用背景色刷新
//pictureBox1填充为白色,便于显示图像 500*300
Rectangle rect = new Rectangle(0, 0, picbox.Width, picbox.Height);
doublebufferg.FillRectangle( new SolidBrush(Color.White), rect);
//画X和Y轴
DrawXY( ref doublebufferg, picbox);
//X轴上的刻度
SetYAxis( ref doublebufferg, picbox, Ymax);
//Y轴上的刻度
SetXAxis( ref doublebufferg, picbox, Xmax);
//要显示的实时曲线部分
Point temp = new Point();
for ( int j = 0; j < picbox.Width / 5 - 1; j++)
{
temp = ptlist[j + 1];
ptlist[j] = new Point(temp.X - 5, temp.Y);
}
Point lastpt = new Point();
lastpt.X = picbox.Width;
lastpt.Y = (picbox.Height -30) -Convert.ToInt32(atemp_mun)*5 ;
ptlist[picbox.Width / 5 - 1] = lastpt;
doublebufferg.DrawLines( new Pen(color, 1), ptlist);
//将缓冲中的位图绘制到我们的窗体上
Graphics g1 = picbox.CreateGraphics(); //创建 PictureBox窗体的画布
g1.Clear(Color.White);
g1.DrawImage(mybitmap, 0, 0);
}
//完成X轴和Y轴的基本部分
public void DrawXY( ref Graphics g, PictureBox picbox)
{
Pen pen = new Pen(Color.Black, 2); //画笔
Pen pen_heigh = new Pen(Color.Red, 1); //画笔
Pen pen_low = new Pen(Color.Gray, 1); //画笔
SolidBrush sb = new SolidBrush(Color.Black); //话刷
SolidBrush sb2 = new SolidBrush(Color.Gray); //话刷
//X轴的箭头,实际上是绘制了一个三角形
Point[] xpts = new Point[3] {
new Point(picbox.Width - 35, picbox.Height - 32),
new Point(picbox.Width - 35, picbox.Height - 28),
new Point(picbox.Width - 30, picbox.Height - 30)
};
g.DrawLine(pen, 40, picbox.Height - 30, picbox.Width - 30, picbox.Height - 30);
g.DrawPolygon(pen, xpts);
g.DrawString( "取样点", new Font( "宋体", 9), sb, picbox.Width - 55, picbox.Height - 45);
//画上上限温度:e
g.DrawLine(pen_heigh, 40, picbox.Height - heigh_temp*5 - 30, picbox.Width - 5, picbox.Height - heigh_temp*5 - 30);
g.DrawString( "上限温度" + heigh_temp.ToString() + "℃", new Font( "宋体", 8), sb, picbox.Width - 80, picbox.Height - heigh_temp * 5 - 45);
//画上下限温度:
g.DrawLine(pen_low, 40, picbox.Height - low_temp * 5 - 30, picbox.Width -5, picbox.Height - low_temp * 5 - 30);
g.DrawString( "下限温度" + low_temp.ToString() + "℃", new Font( "宋体", 8), sb, picbox.Width - 80, picbox.Height - low_temp * 5 - 45);
//Y轴的箭头,实际上是绘制了一个三角形
Point[] ypts = new Point[3] {
new Point(7, 15),
new Point(1, 10),
new Point(3, 15) };
g.DrawLine(pen, 1, picbox.Height - 30, 1, 10);
g.DrawPolygon(pen, ypts);
g.DrawString( "实时温度℃", new Font( "宋体", 9), sb, 14, 10);
}
//绘制Y轴上的刻度25-35
public void SetYAxis( ref Graphics g, PictureBox picbox, float YMAX)
{
Pen p1 = new Pen(Color.Goldenrod, 1);
Pen p2 = new Pen(Color.Black, 2);
SolidBrush sb = new SolidBrush(Color.Black);
float ykedu = YMAX / 200; //给定的最大刻度与实际像素的比例关系
//第一个刻度的两个端点
float xl = 27, yl = picbox.Height - 30, yr = picbox.Height - 30;
for ( int j = 0; j < picbox.Height - 60; j += 10)
{
if (j % 50 == 0) //一个大的刻度,黑色,每隔50像素一个
{
g.DrawLine(p2, xl, yl - j, 2, yl - j); //刻度线
string tempy = (j * ykedu).ToString();
g.DrawString(tempy, new Font( "宋体", 8), sb, 30, yl - j - 5);
}
else //小刻度,金×××,10像素一个
{ g.DrawLine(p1, 10, yl - j, 20, yl - j); }
}
}
Random rm = new Random(); //随机数产生器
public void DrawLineS(Color color, float Xmax, float Ymax, PictureBox picbox, Point[] ptlist)
{
mybitmap = new Bitmap(picbox.Width, picbox.Height); //设定位图大小
Graphics doublebufferg = Graphics.FromImage(mybitmap); //从位图上获取“画布”
doublebufferg.Clear(Color.White); //用背景色刷新
//pictureBox1填充为白色,便于显示图像 500*300
Rectangle rect = new Rectangle(0, 0, picbox.Width, picbox.Height);
doublebufferg.FillRectangle( new SolidBrush(Color.White), rect);
//画X和Y轴
DrawXY( ref doublebufferg, picbox);
//X轴上的刻度
SetYAxis( ref doublebufferg, picbox, Ymax);
//Y轴上的刻度
SetXAxis( ref doublebufferg, picbox, Xmax);
//要显示的实时曲线部分
Point temp = new Point();
for ( int j = 0; j < picbox.Width / 5 - 1; j++)
{
temp = ptlist[j + 1];
ptlist[j] = new Point(temp.X - 5, temp.Y);
}
Point lastpt = new Point();
lastpt.X = picbox.Width;
lastpt.Y = (picbox.Height -30) -Convert.ToInt32(atemp_mun)*5 ;
ptlist[picbox.Width / 5 - 1] = lastpt;
doublebufferg.DrawLines( new Pen(color, 1), ptlist);
//将缓冲中的位图绘制到我们的窗体上
Graphics g1 = picbox.CreateGraphics(); //创建 PictureBox窗体的画布
g1.Clear(Color.White);
g1.DrawImage(mybitmap, 0, 0);
}
//完成X轴和Y轴的基本部分
public void DrawXY( ref Graphics g, PictureBox picbox)
{
Pen pen = new Pen(Color.Black, 2); //画笔
Pen pen_heigh = new Pen(Color.Red, 1); //画笔
Pen pen_low = new Pen(Color.Gray, 1); //画笔
SolidBrush sb = new SolidBrush(Color.Black); //话刷
SolidBrush sb2 = new SolidBrush(Color.Gray); //话刷
//X轴的箭头,实际上是绘制了一个三角形
Point[] xpts = new Point[3] {
new Point(picbox.Width - 35, picbox.Height - 32),
new Point(picbox.Width - 35, picbox.Height - 28),
new Point(picbox.Width - 30, picbox.Height - 30)
};
g.DrawLine(pen, 40, picbox.Height - 30, picbox.Width - 30, picbox.Height - 30);
g.DrawPolygon(pen, xpts);
g.DrawString( "取样点", new Font( "宋体", 9), sb, picbox.Width - 55, picbox.Height - 45);
//画上上限温度:e
g.DrawLine(pen_heigh, 40, picbox.Height - heigh_temp*5 - 30, picbox.Width - 5, picbox.Height - heigh_temp*5 - 30);
g.DrawString( "上限温度" + heigh_temp.ToString() + "℃", new Font( "宋体", 8), sb, picbox.Width - 80, picbox.Height - heigh_temp * 5 - 45);
//画上下限温度:
g.DrawLine(pen_low, 40, picbox.Height - low_temp * 5 - 30, picbox.Width -5, picbox.Height - low_temp * 5 - 30);
g.DrawString( "下限温度" + low_temp.ToString() + "℃", new Font( "宋体", 8), sb, picbox.Width - 80, picbox.Height - low_temp * 5 - 45);
//Y轴的箭头,实际上是绘制了一个三角形
Point[] ypts = new Point[3] {
new Point(7, 15),
new Point(1, 10),
new Point(3, 15) };
g.DrawLine(pen, 1, picbox.Height - 30, 1, 10);
g.DrawPolygon(pen, ypts);
g.DrawString( "实时温度℃", new Font( "宋体", 9), sb, 14, 10);
}
//绘制Y轴上的刻度25-35
public void SetYAxis( ref Graphics g, PictureBox picbox, float YMAX)
{
Pen p1 = new Pen(Color.Goldenrod, 1);
Pen p2 = new Pen(Color.Black, 2);
SolidBrush sb = new SolidBrush(Color.Black);
float ykedu = YMAX / 200; //给定的最大刻度与实际像素的比例关系
//第一个刻度的两个端点
float xl = 27, yl = picbox.Height - 30, yr = picbox.Height - 30;
for ( int j = 0; j < picbox.Height - 60; j += 10)
{
if (j % 50 == 0) //一个大的刻度,黑色,每隔50像素一个
{
g.DrawLine(p2, xl, yl - j, 2, yl - j); //刻度线
string tempy = (j * ykedu).ToString();
g.DrawString(tempy, new Font( "宋体", 8), sb, 30, yl - j - 5);
}
else //小刻度,金×××,10像素一个
{ g.DrawLine(p1, 10, yl - j, 20, yl - j); }
}
}
转载于:https://blog.51cto.com/liuzhengweiyi/338246