基本上我正在做的是使用NAudio来获取峰值信息。我制作了一组20张代表EQ的图像。我已经做了一些代码来检查峰值并使用计时器实时更改图片框中的图像。虽然有时我看到视觉滞后,但我认为代码效率很低。是否存在比这一大块ifs更有效的代码
private void EQ_TIMER_Tick(object sender, EventArgs e)
{
int vol = (int)((float)(device.AudioMeterInformation.MasterPeakValue * 100));
if ((vol > 0) && (vol < 5))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ5;
}
else if ((vol > 5) && (vol < 10))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ10;
}
else if ((vol > 10) && (vol < 15))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ15;
}
else if ((vol > 15) && (vol < 20))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ20;
}
else if ((vol > 20) && (vol < 25))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ25;
}
else if ((vol > 25) && (vol < 30))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ30;
}
else if ((vol > 30) && (vol < 35))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ35;
}
else if ((vol > 35) && (vol < 40))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ40;
}
else if ((vol > 40) && (vol < 45))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ45;
}
else if ((vol > 45) && (vol < 50))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ50;
}
else if ((vol > 50) && (vol < 55))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ55;
}
else if ((vol > 55) && (vol < 60))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ60;
}
else if ((vol > 60) && (vol < 65))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ65;
}
else if ((vol > 65) && (vol < 70))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ70;
}
else if ((vol > 70) && (vol < 75))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ75;
}
else if ((vol > 75) && (vol < 80))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ80;
}
else if ((vol > 80) && (vol < 85))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ85;
}
else if ((vol > 85) && (vol < 90))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ90;
}
else if ((vol > 90) && (vol < 95))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ95;
}
else if ((vol > 95) && (vol < 100))
{
WINGS_FRAME.Image = Properties.Resources.RES_EQ100;
}
else if (vol == 0)
{
WINGS_FRAME.Image = null;
}
}
答案 0 :(得分:2)
尝试这样的事情:
int vol = 38;
int imgnum = vol/5*5;
string imgname = string.Format("RES_EQ{0}", imgnum);
var image = Properties.Resources.ResourceManager.GetObject(imgname);
整数除法将音量“舍入”为步数,然后乘以5得出图像的实际数。