简体   繁体   中英

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. I want the timer to change the image every 3 seconds. this is the timer code I'm using. it works well before to click buttons every 3 seconds but not working with imagelist. 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. it seems cant get it any bigger than 320,320. know how to make it can use bigger resolution size?

Simply move int i out of the while loop.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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