C# 使用picturebox绘制波形文件
我想知道以下代码中的错误。我想绘制包含wave文件样本的数组的值。在表格中,我放置了面板,并将其放入picturebox中C# 使用picturebox绘制波形文件,c#,.net,winforms,gdi+,C#,.net,Winforms,Gdi+,我想知道以下代码中的错误。我想绘制包含wave文件样本的数组的值。在表格中,我放置了面板,并将其放入picturebox中 private void button1_Click(object sender, EventArgs e) { string ss = "test.wav"; double[] xxwav = prepare(ss); int xmin = 300; int ymin = 250; int xmax = 1024; int ymax = 450;
private void button1_Click(object sender, EventArgs e)
{
string ss = "test.wav";
double[] xxwav = prepare(ss);
int xmin = 300; int ymin = 250; int xmax = 1024; int ymax = 450;
int xpmin = 0; int xpmax = xxwav.Length; int ypmin = 32767; int ypmax = -32768;
double a = (double)((xmax - xmin)) /(double) (xpmax - xpmin);
double b = (double)(xpmin - (a * xmin));
double c = (double)((ymax - ymin) /(double) (ypmax - ypmin));
double d = (double)(ypmin - (c * ymin));
double xp1,yp1,xp2,yp2;
Pen redPen = new Pen(Color.Red, 1);
Bitmap bmp = new Bitmap(40000, 500);
Graphics g = Graphics.FromImage(bmp);
PointF p1;
PointF p2;
for (int i = 1; i < xxwav.Length; i++)
{
xp1 = a * (i-1) + b;
yp1 = c * xxwav[i-1] + d;
xp2=a * i + b;
yp2=c * xxwav[i] + d;
p1 =new PointF ((float)xp1,(float)yp1);
p2 =new PointF ((float)xp2,(float)yp2);
g.DrawLine(redPen, p1, p2);
}
pictureBox1.Image = bmp;
MessageBox.Show("complete");
}
public static Double[] prepare(String wavePath)
{
Double[] data;
byte[] wave;
byte[] sR = new byte[4];
System.IO.FileStream WaveFile = System.IO.File.OpenRead(wavePath);
wave = new byte[WaveFile.Length];
data = new Double[(wave.Length - 44) / 4];//shifting the headers out of the PCM data;
WaveFile.Read(wave, 0, Convert.ToInt32(WaveFile.Length));//read the wave file into the wave variable
/***********Converting and PCM accounting***************/
for (int i = 0; i < data.Length; i++)
{
data[i] = BitConverter.ToInt16(wave, i * 2) / 32768.0;
}
//65536.0.0=2^n, n=bits per sample;
return data;
}
private void按钮1\u单击(对象发送者,事件参数e)
{
string ss=“test.wav”;
双[]xxwav=准备(ss);
int-xmin=300;int-ymin=250;int-xmax=1024;int-ymax=450;
int-xpmin=0;int-xpmax=xxwav.Length;int-ypmin=32767;int-ypmax=-32768;
double a=(double)(xmax-xmin))/(double)(xpmax-xpmin);
双b=(双)(xpmin-(a*xmin));
双c=(双)((ymax-ymin)/(双)(ypmax-ypmin));
双d=(双)(ypmin-(c*ymin));
双xp1,yp1,xp2,yp2;
Pen redPen=新笔(颜色:红色,1);
位图bmp=新位图(40000,500);
Graphics g=Graphics.FromImage(bmp);
p1点;
p2点;
对于(int i=1;i
只有在我摆弄了你的转换和缩放参数之后,你的代码才对我有效
我已将您的代码替换为System.Drawing命名空间中可用的和方法。这确实给了我一个wav文件的视图。您只需替换私有无效按钮1\u单击(对象发送者,事件参数e)
实现
var xxwav = prepare(wavFile);
// determine max and min
var max = (from v in xxwav
select v).Max();
var min = (from v in xxwav
select v).Min();
// what is our Y-axis scale
var mid = (max - min);
Pen redPen = new Pen(Color.Red, 1);
Bitmap bmp = new Bitmap(this.pictureBox1.Size.Width, this.pictureBox1.Size.Height);
Graphics g = Graphics.FromImage(bmp);
// x / y position (y-axis to the middle)
g.TranslateTransform(
0
, this.pictureBox1.Size.Height / 2);
// scaling according to picturebox size
g.ScaleTransform(
(float)this.pictureBox1.Size.Width / (float)xxwav.Length
, (float)this.pictureBox1.Size.Height / ((float)mid));
//first point
var prev = new PointF(0, (float)xxwav[0]);
// iterate over next points
for (int i = 1; i < xxwav.Length; i++)
{
var next = new PointF((float) i , (float) xxwav[i] );
g.DrawLine(redPen, prev, next);
prev = next;
}
pictureBox1.Image = bmp;
var xxwav=准备(wavFile);
//确定最大值和最小值
变量最大值=(从v到xxwav)
选择v.Max();
变量最小值=(从v到xxwav)
选择v.Min();
//我们的Y轴刻度是多少
var mid=(最大值-最小值);
Pen redPen=新笔(颜色:红色,1);
位图bmp=新位图(this.pictureBox1.Size.Width,this.pictureBox1.Size.Height);
Graphics g=Graphics.FromImage(bmp);
//x/y位置(y轴到中间)
g、 翻译形式(
0
,此图为x1.Size.Height/2);
//根据picturebox大小进行缩放
g、 标度变换(
(浮动)此.pictureBox1.Size.Width/(浮动)xxwav.Length
,(float)this.pictureBox1.Size.Height/((float)mid));
//第一点
var prev=新点F(0,(浮动)xxwav[0]);
//迭代下一个点
对于(int i=1;i
您不应该告诉我们错误是什么吗?请不要在标题中指定语言,标记用于此目的。:)这个代码应该在表单上绘制波浪,但它不是。。。我请求你帮助我。。是否有任何屏幕错误?现在画的是什么?或者它不显示错误,什么也不做?有一个错误的屏幕截图或是对工作代码的篡改?它不会显示错误,也不会做任何事情