从值显示 EQ 图像的更有效代码

More efficient code for displaying EQ images from value

基本上我所做的是使用 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;
        }
    }

尝试这样的事情:

int vol = 38;
int imgnum = vol/5*5;
string imgname = string.Format("RES_EQ{0}", imgnum);

var image = Properties.Resources.ResourceManager.GetObject(imgname);

整数除法 "rounds" 体积为步数,然后乘以 5 给出图像的实际数字。