为什么在使用计时器在Picturebox1中播放图像时,播放越来越慢



在我单击按钮时,它是一个快速地显示在 pictureBox1中的图像。但是经过一些图像后,它变得非常慢。它像在慢动作模式下一样,显示一个非常慢的图像。

这是按钮单击事件代码:

private void button5_Click(object sender, EventArgs e)
{
    _files = new List<FileInfo>(); 
    myTrackPanelss1.trackBar1.Value = 0;
    _indx = 0;
    _files.AddRange(_fi);
    _files = _files.OrderBy(f => f.LastWriteTime).ToList();
    button5.ForeColor = Color.Red;
    button6.ForeColor = Color.Black;
    button7.ForeColor = Color.Black;
    timer3.Start();
    button6.Enabled = true;
    button6.Text = "Pause";
    button7.Enabled = true;    
}

变量_indx是全局int

然后the the the tick事件:

private void timer3_Tick(object sender, EventArgs e)
{
    try
    {
        myTrackPanelss1.trackBar1.Maximum = _files.Count;
        myTrackPanelss1.trackBar1.Minimum = 0;
        Image iOLd = this.pictureBox1.Image;
        Image img = Image.FromFile(_files[_indx].FullName);
        myTrackPanelss1.trackBar1.Value = _indx;
        label22.Text = _files[_indx].Name;
        this.pictureBox1.Image = img;
        if (iOLd != null)
            iOLd.Dispose();
        _indx++;
        if (_indx >= _files.Count)
        {
            _indx = 0;
        }
        timer3.Interval = 40;
    }
    catch
    {
    }
}

有时在计时器启动后单击按钮时,它在pictureBox1中显示图像非常慢。有时,它像40间隔一样快速地显示它们,然后在某个时候越来越慢。我无法弄清楚为什么它会慢。

在计时器之外声明什么。实例化变量每个刻度都会引起一些油门。实际上,为什么将这些变量存储在内存中?如果可以的话,请摆脱IOLD和IMG ...

在功能之外声明并实例化:

    myTrackPanelss1.trackBar1.Maximum = _files.Count;
    myTrackPanelss1.trackBar1.Minimum = 0;

计时器刻度:

try {
        this.pictureBox1.Image = Image.FromFile(_files[_indx].FullName);
        timer3.Interval = 40;
        _indx++;
}

最新更新