簡體   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