我如何使计时器可以向前或向后更改pictureBox1中显示的图像

本文关键字:pictureBox1 显示 图像 何使 计时器 可以向 | 更新日期: 2023-09-27 18:26:22

在表格1的顶部,我做了:

private bool farwardbackward;
private FileInfo[] fi;

然后我有4个按钮点击事件:

转发:

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

向后:

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

并停止动画按钮:

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

最后一个按钮是暂停/继续:

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

然后在timer5 tick事件中,它的间隔设置为1000ms,我做了:

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

当我运行程序时,我总是在pictureBox1中看到变量fi的最新/最后一个图像。例如,fi中的第一个图像是000004.gif,最后一个图像是050122.gif我看到050122.gif

现在,当使图像像动画gif样式一样显示在pictureBox1中时,逻辑应该是什么?

如果我第一次点击按钮向前移动,那么现在发生的事情是跳到图像000004.gif,然后显示000005.gif等等…

如果我第一次点击后退按钮,那么它开始从050122.gif返回到050121.gif,以此类推…

问题是,当我向后点击,在一些图像之后,我向前点击按钮,然后它从原来的图像继续前进,但当它到达050122.gif时,它停止了。它应该继续不停地运行。这个想法是让它不停地移动。如果它到达050122.gif,我不希望它停止。如果它到达第一个图像或最后一个图像,请保持循环不停止。但由于某种原因,在050122.gif上,如果我在它向后移动时点击向前按钮,它就会停止。

如果我现在没有错的话,问题是当我在中间单击以更改动画的方向时。然后,当它到达最后一个图像或第一个图像时,它停止。我希望只有点击停止按钮,它才能不间断地继续。

另一件事是我应该在停止按钮点击事件中做什么?我应该重置一些值或变量吗?

这是timer5 tick事件代码:

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

当点击向前或向后移动时,我得到了同样的异常:索引在数组的边界之外

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: 

1317行为:

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

我如何使计时器可以向前或向后更改pictureBox1中显示的图像

我认为这将是一个更好的tick方法代码:

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();

除此之外,我会稍微改变一下你的两个按钮的方法。

  • 停止计时器
  • 反转你的bool
  • 重新启动计时器如果你使用这个代码,你也可以删除第二个按钮,只使用一个!您只需将其文本更改为

@γηράσκωδ'αείπιλάδιδασκίμε:thx,我错过了

我的解决方案以这种方式运行良好:


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

没有完美的解决方案,但它做了它应该做的!