Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 生成类似声云c的波浪图像#_C# 4.0_Naudio_Waveform - Fatal编程技术网

C# 4.0 生成类似声云c的波浪图像#

C# 4.0 生成类似声云c的波浪图像#,c#-4.0,naudio,waveform,C# 4.0,Naudio,Waveform,当我试图通过将mp3流传递给以下函数来生成波形图像时,它会在g.DrawLine调用时引发溢出异常。请纠正我的错误。任何帮助都将不胜感激 public static void DrawNormalizedAudio(ref float[] data, Color color) { Bitmap bmp= new Bitmap(1800,200); int BORDER_WIDTH = 5; int width = bmp.Width - (2 * BO

当我试图通过将mp3流传递给以下函数来生成波形图像时,它会在g.DrawLine调用时引发溢出异常。请纠正我的错误。任何帮助都将不胜感激

public static void DrawNormalizedAudio(ref float[] data, Color color)
{
   Bitmap bmp= new Bitmap(1800,200);            

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

        bmp.Save("D:\\waveimage.png"); 
    }

    public float[] FloatArrayFromStream(System.IO.MemoryStream stream)
    {
        return FloatArrayFromByteArray(stream.GetBuffer());
    }

    public float[] FloatArrayFromByteArray(byte[] input)
    {
        float[] output = new float[input.Length / 4];
        for (int i = 0; i < output.Length; i++)
        {
            output[i] = BitConverter.ToSingle(input, i * 4);
        }
        return output;
    }      
public static void DrawNormalizedAudio(ref float[]数据,颜色)
{
位图bmp=新位图(1800200);
内部边界宽度=5;
int width=bmp.width-(2*边框宽度);
int height=bmp.height-(2*边框宽度);
使用(Graphics g=Graphics.FromImage(bmp))
{
g、 清晰(颜色为黑色);
钢笔=新钢笔(颜色);
int size=data.Length;
对于(int-iPixel=0;iPixelmax?val:max;
}
int yMax=边框宽度+高度-(int)((最大+1)*.5*高度);
int-yMin=边框宽度+高度-(int)((最小值+1)*.5*高度);
g、 抽绳(钢笔、iPixel+边框宽度、yMax、,
iPixel+边框宽度,yMin);
}
}
bmp.Save(“D:\\waveimage.png”);
}
公共浮点[]FloatArrayFromStream(System.IO.MemoryStream流)
{
返回FloatArrayFromByteArray(stream.GetBuffer());
}
公共浮点[]浮点数组FromByteArray(字节[]输入)
{
float[]输出=新的float[input.Length/4];
for(int i=0;i
我的第一个想法是,您正在绘制一个输出列多于输入列的图像,这将为您提供超出范围的
min
max
(因此
yMin
yMax
)值。我希望在这种情况下,
yMin
计算会抛出异常

您应该将
yMin
yMax
值夹紧到画布上绘图区域的大小,然后在调用
g.DrawLine
之前(或期间)添加
边框宽度
偏移量