简体   繁体   中英

How can I make timer that will change the images display in pictureBox1 forward or backward?

In top of form1 i did:

private bool farwardbackward;
private FileInfo[] fi;

Then I have 4 buttons click events:

Forward:

private void button4_Click(object sender, EventArgs e)
        {
            farwardbackward = true;
            button5.Enabled = true;
            button6.Enabled = true;
            timer5.Start();
        }

And backward:

private void button3_Click(object sender, EventArgs e)
        {
            farwardbackward = false;
            button5.Enabled = true;
            button6.Enabled = true;
            if (current == -1)
                current = fi.Length;
            timer5.Start();
        }

And stop animation button:

private void button6_Click(object sender, EventArgs e)
        {
            timer5.Stop();
            button6.Enabled = false;
            button5.Text = "Pause Animation";
            button5.Enabled = false;
            Bitmap lastdownloadedimage = new Bitmap(fi[fi.Length -1].FullName);
            pictureBox1.Image = lastdownloadedimage;
        }

And last button is pause/continue:

private void button5_Click(object sender, EventArgs e)
        {
            b = (Button)sender;

            if (b.Text == "Pause Animation")
            {
                timer5.Enabled = false;
                b.Text = "Continue Animation";
            }
            else
            {
                timer5.Enabled = true;
                b.Text = "Pause Animation";
            }
        }

And then in timer5 tick event that it's interval set to 1000ms I did:

int current = -1;
        private void timer5_Tick(object sender, EventArgs e)
        {
            if (farwardbackward)
                current = Math.Min(current + 1, fi.Length - 1);
            else
                current = Math.Max(current - 1, 0);

            if (fi.Length > 0)
            {

               Bitmap newbmp = new Bitmap(fi[current].FullName);
               pictureBox1.Image = newbmp;
               pictureBox1.Refresh();
               newbmp.Dispose();
            }            
        }

When I'm running my program I always see in the pictureBox1 the latest/last image from the variable fi. For example the first image in fi is 000004.gif and the last image is 050122.gif I see 050122.gif

Now what should be the logic when making the images to be display in the pictureBox1 like animated gif style ?

If first time I click on the button to move forward so what happen now is it's jumping to image 000004.gif and then show 000005.gif and so on...

If first time I click on backward button then it's starting go back from 050122.gif to 050121.gif and so on...

The problem is when I click on backward and after some images I click on the button forward then it continue from the image it was and move forward but then when it's getting to 050122.gif it stop. and it should continue non stop. The idea is to keep it moving nonstop. I don't want it to stop if it's getting to 050122.gif. If it's getting to the first image or the last image just keep looping it non stop. But for some reason on 050122.gif it stop if I click on farward button while it's moving backward.

The problem if I'm not wrong now is when I click in the middle to change the direction of the animation. Then when it's getting to the last image or the first image it stop. And I want it to continue nonstop only if I click on the stop button.

Another thing is wat should i do in the stop button click event ? Shouldi reset some values or variables ?

This is the timer5 tick event code:

int current = -1;
        private void timer5_Tick(object sender, EventArgs e)
        {
            if (fi.Length > 0)
            {
                if (farwardbackward)
                {
                    current = (current >= fi.Length - 1) ? 0 : current++;
                }
                else
                {
                    current = (current <= 0) ? fi.Length - 1 : current--;
                }
                pictureBox1.Image = new Bitmap(fi[current].FullName);
                pictureBox1.Refresh();
            }
        }

When clicking to move forward or backward i'm getting same exception: Index was outside the bounds of the array

System.IndexOutOfRangeException was caught
  HResult=-2146233080
  Message=Index was outside the bounds of the array.
  Source=My Weather Station
  StackTrace:
       at mws.Form1.timer5_Tick(Object sender, EventArgs e) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Form1.cs:line 1317
       at System.Windows.Forms.Timer.OnTick(EventArgs e)
       at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at mws.Program.Main() in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Program.cs:line 28
  InnerException: 

Line 1317 is:

pictureBox1.Image = new Bitmap(fi[current].FullName);

I think this would be a better code for your tick method:

if(farwardbackward){
current = (current >= fi.Length-1) ? 0 : current++;
}else{
current = (current <= 0) ? fi.Length-1 : current--;
}
picturebox1.Image = new Bitmap(fi[current].Fullname);
picturebox1.Refresh();

Besides this I would change your two button's methods a little bit.

  • Stop your timer
  • invert your bool
  • start your timer again If you use this code you can also delete the second button and only use one! You just have to change its text to.

@γηράσκω δ' αεί πολλά διδασκόμε: thx, I missed that point

My solution works well this way:


public partial class Form1 : Form
    {
        private string[] fi;
        private int current = 0;
        private Boolean forwardbackward = true;
        private Boolean timerRunning = true;

public Form1() { InitializeComponent(); fi = Directory.GetFiles(@"C://temp/pics"); timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { if (fi.Length > 0) { if (forwardbackward) { current = (current >= fi.Length - 1) ? 0 : ++current; } else { current = (current <= 0) ? fi.Length - 1 : --current; } pictureBox1.Image = new Bitmap(fi[current]); pictureBox1.Refresh(); } } private void btn_changeDirection_Click(object sender, EventArgs e) { timer1.Stop(); forwardbackward = (forwardbackward) ? false : true; timer1.Start(); } private void btn_Pause_Continue_Click(object sender, EventArgs e) { if (timerRunning) { timer1.Stop(); timerRunning = false; btn_Pause_Continue.Text = "Continue"; } else { timer1.Start(); timerRunning = true; btn_Pause_Continue.Text = "Pause"; } } private void btn_stop_Click(object sender, EventArgs e) { timer1.Stop(); } }

No perfect solution, but it does what it should!

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