简体   繁体   English

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

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

In top of form1 i did: 在form1顶部,我做了:

private bool farwardbackward;
private FileInfo[] fi;

Then I have 4 buttons click events: 然后,我有4个按钮单击事件:

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: 然后在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();
            }            
        }

When I'm running my program I always see in the pictureBox1 the latest/last image from the variable fi. 当我运行程序时,我总是在pictureBox1中看到变量fi的最新/最新图像。 For example the first image in fi is 000004.gif and the last image is 050122.gif I see 050122.gif 例如,fi中的第一个图像是000004.gif,最后一个图像是050122.gif,我看到050122.gif

Now what should be the logic when making the images to be display in the pictureBox1 like animated gif style ? 现在,当使图像以动画gif样式显示在pictureBox1中时,逻辑应该是什么?

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... 如果是我第一次单击按钮前进,那么现在发生的是它跳转到图像000004.gif,然后显示000005.gif,依此类推...

If first time I click on backward button then it's starting go back from 050122.gif to 050121.gif and so on... 如果是第一次单击后退按钮,那么它将从050122.gif返回到050121.gif,依此类推...

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. 问题是当我向后单击时,在某些图像后单击向前按钮,然后从原来的图像继续并向前移动,但是当到达050122.gif时,它停止了。 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. 如果到达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. 但是由于某种原因,如果在050122.gif上向后移动时单击前进按钮,它将停止。

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: 这是timer5滴答事件代码:

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: 1317行是:

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

I think this would be a better code for your tick method: 我认为这对于您的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();

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 @γηράσκωδ'αείπολλάδιδασκόμε:thx,我错过了这一点

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! 没有完美的解决方案,但是它确实可以做到!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在将鼠标移到pictureBox1上时自动在pictureBox2上绘制点? - How can i make that when i move the mouse ober pictureBox1 it will draw points automatic on pictureBox2? 在Timer方法中更改pictureBox1属性 - Change pictureBox1 properties in Timer method 如何将pictureBox1中的图像替换为pictureBox1矩形区域上绘制的裁剪图像? - How can i replace the image in pictureBox1 with a crop image of a drawn on the pictureBox1 rectangle area? 如何使用factor变量使位图适合pictureBox1? - How can I use the factor variable to make the bitmap to fit in the pictureBox1? 如何从硬盘加载图像并在带定时器的图片框中显示它们? - How can I load images from the hard disk and display them in pictureBox with timer? 如果用户处于鼠标按下事件中,如何检查pictureBox1鼠标离开事件? - How can i check on pictureBox1 mouse leave event if the user is in mouse down event? 我无法从 C# 中的代码更改 pictureBox1 中的图像 - I cannot change the image in pictureBox1 from code in C# 我如何计算从pictureBox1的顶部到form1的顶部的距离? - How can i calculate the distance from the top of pictureBox1 and almost the top of form1? 移动trackBar1时,如何保持在图片盒1中的图像上绘制的点? - How can i keep the points im drawing on the image in the picturebox1 when moving the trackBar1? 如何解决picturebox1是null? - How to solve picturebox1 is null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM