使用此:
public static void DrawNormalizedAudio(ref float[] data, PictureBox pb,
Color color)
{
Bitmap bmp;
if (pb.Image == null)
{
bmp = new Bitmap(pb.Width, pb.Height);
}
else
{
bmp = (Bitmap)pb.Image;
}
int BORDER_WIDTH = 5;
int width = bmp.Width - (2 * BORDER_WIDTH);
int height = bmp.Height - (2 * BORDER_WIDTH);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Black);
Pen pen = new Pen(color);
int size = data.Length;
for (int iPixel = 0; iPixel < width; iPixel++)
{
// determine start and end points within WAV
int start = (int)((float)iPixel * ((float)size / (float)width));
int end = (int)((float)(iPixel + 1) * ((float)size / (float)width));
float min = float.MaxValue;
float max = float.MinValue;
for (int i = start; i < end; i++)
{
float val = data[i];
min = val < min ? val : min;
max = val > max ? val : max;
}
int yMax = BORDER_WIDTH + height - (int)((max + 1) * .5 * height);
int yMin = BORDER_WIDTH + height - (int)((min + 1) * .5 * height);
g.DrawLine(pen, iPixel + BORDER_WIDTH, yMax,
iPixel + BORDER_WIDTH, yMin);
}
}
pb.Image = bmp;
}
我在这一行收到错误:
g.DrawLine(pen, iPixel + BORDER_WIDTH, yMax,
iPixel + BORDER_WIDTH, yMin);
它表示操作溢出(不能除以零)或类似的东西。关于这个问题的任何线索?感谢。
更新: 我用来调用函数的代码是:
fileName = "c:\\sound\\happy_birthday.wav";
byte[] bytes = File.ReadAllBytes(fileName);
float[] getval = FloatArrayFromByteArray(bytes);
DrawNormalizedAudio(ref getval, pictureBox1, Color.White);
答案 0 :(得分:0)
你除以零。不要这样做,因为此操作无效。检查传递给DrawLine
的值不是0.
答案 1 :(得分:0)
int yMin = BORDER_WIDTH + height - (int)((min + 1) * .5 * height);
您正在使用signed int数据类型 - 会发生的情况是此计算溢出并导致负值,您得到-2147483572
int.MaxValue+75
(溢出),可能是min
1}}是一个较大的负值,导致结果比int.MaxValue
大75?
答案 2 :(得分:0)
min
和max
错误,应该是
float min = float.MinValue;
float max = float.MaxValue;