之前发过一个频谱分析的计算类,
现在写了个方法把数据描绘在窗体控件中
很简单
窗体中有picturebox控件name:pictureBox1
其他属性:
width 640
Height 400
每40个像素分一个小格子吧
这个方法是画个背景坐标轴
void draw(int x1, int y1, int[] xbuf, int[] ybuf)
{
//创建位图
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
//清空图片背景色
g.Clear(Color.White);
Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
Font font1 = new System.Drawing.Font("宋体", 20, FontStyle.Regular);
Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.Red, 1.2f, true);
//绘制背景
g.FillRectangle(Brushes.White, 0, 0, width, height);
//Brush brush1 = new SolidBrush(Color.Blue);
//Brush brush2 = new SolidBrush(Color.SaddleBrown);
//画图片的边框线
g.DrawRectangle(new Pen(Color.Black), 20, 20, 600, 320);
//g.DrawRectangle(new Pen(Color.Black), 40, 35, 890, 490);
System.Drawing.Pen mypen = new Pen(brush, 1);
mypen.DashStyle = DashStyle.Dot;
System.Drawing.Pen mypen2 = new Pen(Color.Red, 2);
System.Drawing.Pen mypen3 = new Pen(Color.Yellow, 1);
//纵向线条
int x = 60;
for (int i = 0; i < 14; i++)
{
g.DrawLine(mypen, x, 20, x, 340);
x = x + 40;
}
//横向线条
int y = 60;
for (int i = 0; i < 7; i++)
{
g.DrawLine(mypen, 20, y, 620, y);
y = y + 40;
}
//x轴上对应的标记
String[] n = { " 0"," 2", " 4", " 6", " 8", " 10", " 12", " 14"," 16", " 18",
" 20", "22", "24", "26", "28", "30"};
x = 10;
for (int i = 0; i < 16; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Black, x, 340); //设置文字内容及输出位置
x = x + 40;
}
//X轴单位
g.DrawString("频率(Hz)", font, Brushes.Black, 580, 350);
g.DrawString("10mm/2Hz 10mm/5mW", font, Brushes.Black, 0, 350);
//y轴上对应的标记
String[] m = {"40","35", "30","25","20","15","10",
" 5"};
y = 20;
for (int i = 0; i < 8; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Black, 2, y); //设置文字内容及输出位置
y = y + 40;
}
//Y轴单位
g.DrawString("功率(mW)", font, Brushes.Black, 2, 2);
if (drawflag == 1)
{
for (int i = 0; i < readlen / 2; i++)
{
g.DrawEllipse(mypen2, xbuf[i] - 2, ybuf[i] - 2, 4, 4);
if (i < readlen / 2 - 1)
g.DrawLine(mypen3, xbuf[i], ybuf[i], xbuf[i + 1], ybuf[i + 1]);
}
}
if (flag == true)
{
g.DrawLine(mypen2, x1, 41, x1, 310);
g.DrawLine(mypen2, 41, y1, 760, y1);
}
g.Dispose();
this.pictureBox1.Image = image;
}
然后取了数据.在原先的坐标轴上面描点
方法中描点的过程很简单.在时机应用的时候,要仔细计算比例
比如最大值不超过高度的2/3,很坐标纵坐标的对应都是要考虑的
private void PicPaintGxx(ref double[] data, ref PictureBox picbox)
{
//x轴比例
int pointnum = 600;
int y = 320;
double xsamplerate = (double)data.Length / pointnum; //temp.Length / pointnum;
//y轴比例
double yscale = y * 2 / 3 / GetMax(ref data);
PointF[] points = new PointF[pointnum];
for (int i = 0; i < pointnum; i++)
{
points[i].X = i + 20;
points[i].Y = (float)(340-data[Convert.ToInt32(Math.Floor(i * xsamplerate))] * yscale);
}
//Bitmap bmp = new Bitmap(picbox.Width, picbox.Height);
Bitmap bmp = new Bitmap(this.pictureBox1.Image);
Graphics gr = Graphics.FromImage((Image)bmp);
Pen mypen = new Pen(Color.Blue);
gr.DrawLines(mypen, points);
picbox.Image = bmp;
}
private double GetMax(ref double[] data)
{
double max = 0;
foreach (double x in data)
if (x > max)
max = x;
return max;
}
如果要清除图像的话.再调用一次加载坐标轴的方法就好了.其实是等于重新绘制一次坐标轴