在PictureBox的每一秒C#更改图片 [英] Change image in picturebox every second C#

查看:617
本文介绍了在PictureBox的每一秒C#更改图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个WinForm应用程序,需要一个人与一个摄像头的照片,我试图立即创建倒计时效果。我有4张图片,我想通过循环,但这被证明是相当棘手的完成。



我使用秒的定时器,但这一切发生的是应用滞后了一下,然后最后的图像显示。有谁知道我怎么可能做到这一点。



下面是我的代码:

  INT计数器= 0; 
//启动计数器以交换图像
tmCountDown.Start();
,而(计数器4;)
{
//持观望拍照
}
//定时器
计数器= 0复位计数器;
tmCountDown.Stop();

///<总结>
///计时器事件倒计时图像
///℃之间进行切换; /总结>
///< PARAM NAME =发件人>< /参数>
///< PARAM NAME =E>< /参数>
私人无效tmCountDown_Tick(对象发件人,EventArgs五)
{
计数器++;
//MessageBox.Show(\"c:/vrfid/apppics/+专柜+.JPG);
pbCountDown.Image =新位图(C:/ vrfid / apppics /+专柜+.JPG);
}


解决方案

在Windows Timer类使用用于通知定时器消息队列已过期。所以你需要有为了得到定时器正确数量的过期发生的历史运行的消息循环。所以,你应该设置计数器变量是一个类字段,然后你可以增加它在事件处理中。事情是这样的......

  //主代码
_counter = 0;
tmCountDown.Start();

//事件处理
私人无效tmCountDown_Tick(对象发件人,EventArgs五)
{
_counter ++;
如果(_counter == 4)
tmCountDown.Stop();
,否则
pbCountDown.Image =新位图(C:/ vrfid / apppics /+ _counter +.JPG);
}


I'm creating a WinForm application that takes a person's photo with a webcam and am trying to now create a countdown effect. I have 4 images that i would like to cycle through but this is proving quite tricky to accomplish.

I'm using a timer for the seconds, but all that's happening is the app lags a bit and then the last image shows. Does anybody know how i might accomplish this?

Here's my code:

        int counter = 0;
        // start the counter to swap the images
        tmCountDown.Start();
        while (counter < 4)
        {
            // holding off picture taking
        }
        // reset counter for timer
        counter = 0;
        tmCountDown.Stop();

    /// <summary>
    /// timer event to switch between the countdown images
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void tmCountDown_Tick(object sender, EventArgs e)
    {
        counter++;
        //MessageBox.Show("c:/vrfid/apppics/" + counter + ".jpg");
        pbCountDown.Image = new Bitmap("c:/vrfid/apppics/" + counter + ".jpg");
    }

解决方案

The Windows Timer class uses the message queue for notifying the timer has expired. And so you need to have the message loop running in order to get the correct number of timer expires occuring. So you should set the counter variable to be a class field and then you can increment it inside the event handler. Something like this...

    // Main Code
    _counter = 0;
    tmCountDown.Start();

    // Event Handler
    private void tmCountDown_Tick(object sender, EventArgs e)    
    {
        _counter++;
        if (_counter == 4)
            tmCountDown.Stop();
        else
            pbCountDown.Image = new Bitmap("c:/vrfid/apppics/" + _counter + ".jpg");
    }

这篇关于在PictureBox的每一秒C#更改图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆