繁体   English   中英

如何在 C# 中使用图像列表和计时器制作自动图像幻灯片?

[英]How to make automated image slideshow using imagelist & timer in C#?

如何制作幻灯片以自动更改图片框中的图片? 目前我正在使用 imagelist 但它似乎没有用。 我希望计时器每 3 秒更改一次图像。 这是我正在使用的计时器代码。 在每 3 秒单击一次按钮但不适用于图像列表之前,它运行良好。 我是图像列表和幻灯片的新手,所以如果有任何建议,请告诉我。 谢谢。

        private bool _timerEnabled;
        private async Task StartTimer()
        {
            _timerEnabled = true;
                int i = 0;
                while (_timerEnabled)
                {
                    i++;
                    if (i > 2) { i = 0; }
                    pictureBox2.Image = imageList1.Images[i];
                    bmp = new Bitmap(pictureBox2.Image, pictureBox2.Width, pictureBox2.Height);
                    pictureBox1.Refresh();
                    pictureBox2.Refresh();
                    await Task.Delay(3000);
                }
        }
        private async void timerStartButton_Click(object sender, EventArgs e)
        {
            timerStopButton.Enabled = true;
            timerStartButton.Enabled = false;
            if (_timerEnabled)
                return;
            await StartTimer();
        }
        private void timerStopButton_Click(object sender, EventArgs e)
        {
            timerStopButton.Enabled = false;
            timerStartButton.Enabled = true;
            _timerEnabled = false;
        }

幻灯片现在工作正常,但使用的图像变得模糊。 如何解决这个问题?

原图在此处输入图片说明

结果模糊在此处输入图片说明

编辑。 再次检查后,模糊结果来自自动将图像大小设置为 16,16 的图像列表控件。 它似乎不能超过 320,320。 知道如何使它可以使用更大的分辨率尺寸吗?

只需将int i移出while循环。

private async Task StartTimer() {
    _timerEnabled = true; 
    int i = 0; //Move this here
    while (_timerEnabled) { 
        i++;
        if (i > 2) { i = 0; }
        pictureBox2.Image =  imageList1.Images[i];
        pictureBox1.Refresh();
        pictureBox2.Refresh(); 
        await Task.Delay(3000); 
    } 
}

或者您可以使用内置的计时器控件而不是重新发明轮子。

Timer timer1;
int i = 0;

//Form's constructor
public Form1
{
    timer1 = new Timer();
    timer1.Interval = 3000;
    timer1.Tick += new EventHandler(timer1_tick);
}

private void timer1_tick(object sender, EventArgs e)
{
    i++;
    if (i > 2) { i = 0; } 
    pictureBox2.Image = imageList1.Images[i];
    pictureBox1.Refresh();
    pictureBox2.Refresh(); 
} 

private async void timerStartButton_Click(object sender, EventArgs e) { 
    timerStopButton.Enabled = true;
    timerStartButton.Enabled = false; 
    if (timer1.Enabled) return;
    timer1.Enabled = True;
} 

private void timerStopButton_Click(object sender, EventArgs e) { 
    timerStopButton.Enabled = false;
    timerStartButton.Enabled = true;  
    timer1.Enabled = false; 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM