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

本文关键字:图像 EQ 显示 有效 代码 | 更新日期: 2024-09-20 01:55:08

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

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

试试这样的东西:

int vol = 38;
int imgnum = vol/5*5;
string imgname = string.Format("RES_EQ{0}", imgnum);
var image = Properties.Resources.ResourceManager.GetObject(imgname);

整数除法将体积"四舍五入"为步长,然后乘以5得到图像的实际数字。