0

使用这个:

 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);
4

3 回答 3

0

你除以零。不要这样做,因为这个操作是无效的。检查您传递给的值DrawLine不是 0。

于 2011-03-17T09:01:31.903 回答
0

和错了minmax应该是

float min = float.MinValue;
float max = float.MaxValue;
于 2012-10-27T22:55:31.400 回答
0
 int yMin = BORDER_WIDTH + height - (int)((min + 1) * .5 * height);

您正在使用带符号的 int 数据类型 - 发生的情况是此计算溢出并导致负值,您得到-2147483572的是int.MaxValue+75(溢出),是否min是一个大的负值导致结果比 75 大int.MaxValue

于 2011-03-19T04:47:29.467 回答