简体   繁体   English

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

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

How to make slideshow to automaticly changing pictures in picturebox?如何制作幻灯片以自动更改图片框中的图片? currently im using imagelist but it didnt seems to works.目前我正在使用 imagelist 但它似乎没有用。 I want the timer to change the image every 3 seconds.我希望计时器每 3 秒更改一次图像。 this is the timer code I'm using.这是我正在使用的计时器代码。 it works well before to click buttons every 3 seconds but not working with imagelist.在每 3 秒单击一次按钮但不适用于图像列表之前,它运行良好。 I'm new with imagelist & slideshows so if have any suggestion about it please tell me.我是图像列表和幻灯片的新手,所以如果有任何建议,请告诉我。 thanks.谢谢。

        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;
        }

the slideshow works fine now but the used image become blurry.幻灯片现在工作正常,但使用的图像变得模糊。 How to fix this one?如何解决这个问题?

Original Image原图在此处输入图片说明

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

EDIT.编辑。 after checking it again the blurry result is from imagelist control that automaticly set the image size to 16,16.再次检查后,模糊结果来自自动将图像大小设置为 16,16 的图像列表控件。 it seems cant get it any bigger than 320,320.它似乎不能超过 320,320。 know how to make it can use bigger resolution size?知道如何使它可以使用更大的分辨率尺寸吗?

Simply move int i out of the while loop.只需将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); 
    } 
}

Or you can use the inbuilt timer control instead of reinventing the wheel.或者您可以使用内置的计时器控件而不是重新发明轮子。

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