How to create a video background using frames and timer?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sword117
    New Member
    • Jun 2010
    • 35

    How to create a video background using frames and timer?

    Im trying to create a video background using frames from a video, and using a timer to run the frames 30FPS , can someone help me out? its for a school project.

    im here so far, i can make this work with a trackbar, but using a timer to automatically refresh the background 30FPS would be a lot more cool =).

    Code:
     
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Threading;
    
    namespace MyClock
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
               
            }
    
            
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Text = "My Clock";
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                //timer1.Interval = 1000;
                //FPS set up
                double FPS = 30.0;
                
                timer1.Interval = Convert.ToInt32(FPS);
    
                 Image[] images = new Image[2003];
                 for (int i = 1; i < 10; i++)
                 {
                     images[i] = Image.FromFile(@"C:\Users\Sword Master\Desktop\New folder (6)\PS3 Background Waves Attempt HD (08-07-2010 23-45-07)\PS3 Background Waves Attempt HD 000" + i + ".jpg");
                     this.BackgroundImage = images[i];
                 }
               //test if the timer works
                label2.Text = DateTime.Now.ToLongTimeString();
            }
    
        }
    }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    We can't really do your homework for you, but we can help you out.

    What question specifically do you have about a timer?

    Here's something to try... on your form, drop down a timer. Set the interval to say, 1000 (milliseconds, 1 second). Attach a handler to the tick event and put something visible in there, maybe a Console.WriteLi ne("Ticked!");

    Hopefully that's enough hint to get you started :)

    Comment

    • sword117
      New Member
      • Jun 2010
      • 35

      #3
      Originally posted by GaryTexmo
      We can't really do your homework for you, but we can help you out.

      What question specifically do you have about a timer?

      Here's something to try... on your form, drop down a timer. Set the interval to say, 1000 (milliseconds, 1 second). Attach a handler to the tick event and put something visible in there, maybe a Console.WriteLi ne("Ticked!");

      Hopefully that's enough hint to get you started :)
      =) okay i already done that, but my question here is more like how do i put the timer to search the image[i] and show the images on a speed of 30 FPS, but my main problem is how to say that on the timer1_tick(lik e the interval), i really dont know where to start, i tried everything that i found on google and still had no results,please give me a light =)

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Two things...

        1) If you want to run through an image in an array, you'd going to need some kind of counter, right? A class member variable might be appropriate here... What I mean by this is, what is images[i]? You're accessing it via a for loop, right? Well, you kind of want to replace your for loop with the timer itself...

        2) What does FPS actually mean? Frames per second. If you had 30 frames per second, how many seconds would it be per frame? Where on the timer can you enter an amount relating to seconds?

        (Hopefully that nudges ya in the right direction. You've clearly stated what you want to do, you've just gotta play around with a timer to learn how you can use it to your advantage.)

        Comment

        • sword117
          New Member
          • Jun 2010
          • 35

          #5
          Originally posted by GaryTexmo
          Two things...

          1) If you want to run through an image in an array, you'd going to need some kind of counter, right? A class member variable might be appropriate here... What I mean by this is, what is images[i]? You're accessing it via a for loop, right? Well, you kind of want to replace your for loop with the timer itself...

          2) What does FPS actually mean? Frames per second. If you had 30 frames per second, how many seconds would it be per frame? Where on the timer can you enter an amount relating to seconds?

          (Hopefully that nudges ya in the right direction. You've clearly stated what you want to do, you've just gotta play around with a timer to learn how you can use it to your advantage.)
          im using 2003 images, and i want to show them, but if i dont get to control the speed that they are showed i dont see the images, thats why i created a for, but im only doing it to 10 just like a test drive.

          I say 30FPS because it shows 30 frames a second, i see in games that the img flows right, and im trying too do that, it actualy doesnt need to be 30FPS, just the speed need to flow nomal.

          but thanks again =)

          Comment

          • sword117
            New Member
            • Jun 2010
            • 35

            #6
            Originally posted by sword117
            im using 2003 images, and i want to show them, but if i dont get to control the speed that they are showed i dont see the images, thats why i created a for, but im only doing it to 10 just like a test drive.

            I say 30FPS because it shows 30 frames a second, i see in games that the img flows right, and im trying too do that, it actualy doesnt need to be 30FPS, just the speed need to flow nomal.

            but thanks again =)
            Hello again =) i managed to what i want with a scrool bar, but i cannot control the trackbar value... so i thinked of the timer.

            here is the code to create a flow of images.

            Code:
              private void trackBar1_Scroll_1(object sender, EventArgs e)
                    {
                         Image[] images = new Image[2003];
                            for (int i = 1; i < 10; i++)
                            {
                                images[i] = Image.FromFile(@"C:\Users\Sword Master\Desktop\New folder (6)\PS3 Background Waves Attempt HD (08-07-2010 23-45-07)\PS3 Background Waves Attempt HD 000" + i + ".jpg");
            
                                if (trackBar1.Value == i)
                                {
                                    this.BackgroundImage = images[i];
                                }
            
                            }
                            label1.Text = trackBar1.Value.ToString();
                         
                    }

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              im using 2003 images, and i want to show them, but if i dont get to control the speed that they are showed i dont see the images, thats why i created a for, but im only doing it to 10 just like a test drive.

              I say 30FPS because it shows 30 frames a second, i see in games that the img flows right, and im trying too do that, it actualy doesnt need to be 30FPS, just the speed need to flow nomal.

              but thanks again =)
              Like I said, think about what frames per second actually means. That's the number of frames shown in a second. This value is easily converted to how many seconds we show each frame...

              30 frames per seconds --> 1/30 seconds per frame, this is 33.33 milliseconds per frame. There's a place on your timer exactly for this.

              Now what does your timer need to do? It needs to advance your frame counter, then update the drawing. In your case, the background image.

              With the code you've posted, you'll never see more than 10 images cycle and only when the user scrolls the track bar. Keep on with the timer, you'll get there :D Just think about it and before you know it, you'll have one of those "oooooooh" moments.

              By the way, it just so happens that about a year ago I did almost exactly this with a little animation from Mega Man X. You're welcome to see the code, but I'm going to make you figure this out first ;) Trust me, you'll be happier for it.

              Comment

              • sword117
                New Member
                • Jun 2010
                • 35

                #8
                Originally posted by GaryTexmo
                Like I said, think about what frames per second actually means. That's the number of frames shown in a second. This value is easily converted to how many seconds we show each frame...

                30 frames per seconds --> 1/30 seconds per frame, this is 33.33 milliseconds per frame. There's a place on your timer exactly for this.

                Now what does your timer need to do? It needs to advance your frame counter, then update the drawing. In your case, the background image.

                With the code you've posted, you'll never see more than 10 images cycle and only when the user scrolls the track bar. Keep on with the timer, you'll get there :D Just think about it and before you know it, you'll have one of those "oooooooh" moments.

                By the way, it just so happens that about a year ago I did almost exactly this with a little animation from Mega Man X. You're welcome to see the code, but I'm going to make you figure this out first ;) Trust me, you'll be happier for it.
                Thank you very much! =) i made it lol
                but now i have another problem, the form becomes unusable when i click it, or move it, button´s dont work...

                i saw in the toolbox the background worker, if i put this code there will i be able to click on the form?

                thanks a lot =)

                Code:
                private void timer1_Tick(object sender, EventArgs e)
                        {
                            
                            Image[] images = new Image[2003];
                            timer1.Interval = 1000 / 30;
                
                            for (int i = 32; i <= 2001; i++)
                            {
                                if (i <= 2001)
                                {
                                    this.Refresh();
                                    images[i] = Image.FromFile(@"C:\Users\Sword Master\Desktop\New folder (6)\PS3 Background Waves Attempt HD (08-07-2010 23-45-07)\1 (" + i + ").jpg");
                                    this.BackgroundImage = images[i];
                                    label2.Text = i.ToString();
                
                                  
                                }
                                else
                                {
                                    return;
                                }
                  
                            }
                
                        }

                Comment

                • GaryTexmo
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1501

                  #9
                  You're close, but not quite there :)

                  Take a look at what you're doing in your timer tick method. Every 33.33 milliseconds you're setting the timer tick to 33.33 milliseconds, then you're looping through every image in the list.

                  You do not need to do this. Your timer's tick method is what changes the drawn image. Read: You do not need the for loop at all.

                  Also, preload all your images into the image array in the constructor or form load event. Putting it in the timer tick event makes things very slow as every 33 milliseconds you're loading a file from the hard drive.

                  Let me try to put this another way. Your tick event just puts an image in the background. The tick happens whenever the timer's interval expires. The interval controls the framerate of the image drawing.

                  Comment

                  • sword117
                    New Member
                    • Jun 2010
                    • 35

                    #10
                    Originally posted by GaryTexmo
                    You're close, but not quite there :)

                    Take a look at what you're doing in your timer tick method. Every 33.33 milliseconds you're setting the timer tick to 33.33 milliseconds, then you're looping through every image in the list.

                    You do not need to do this. Your timer's tick method is what changes the drawn image. Read: You do not need the for loop at all.

                    Also, preload all your images into the image array in the constructor or form load event. Putting it in the timer tick event makes things very slow as every 33 milliseconds you're loading a file from the hard drive.

                    Let me try to put this another way. Your tick event just puts an image in the background. The tick happens whenever the timer's interval expires. The interval controls the framerate of the image drawing.
                    i think i didnt understand you well xD

                    is it close to this? lol

                    Code:
                            private void Form1_Load(object sender, EventArgs e)
                            {
                                Text = "My Clock";
                                MyClock();
                    
                            }
                    
                            public void MyClock()
                            {
                                Image[] images = new Image[2003];
                                for (int i = 32; i <= 2001; i++)
                                {
                                    images[1] = Image.FromFile(@"Images\1 (" + i + ").jpg");
                                    this.BackgroundImage = images[i];
                                    label2.Text = i.ToString();
                                   
                                }
                            }
                            
                            private void timer1_Tick(object sender, EventArgs e)
                            {
                                 timer1.Interval = 1000 / 30;
                                 this.Refresh();
                            }

                    Comment

                    • GaryTexmo
                      Recognized Expert Top Contributor
                      • Jul 2009
                      • 1501

                      #11
                      How come literally all of your data is local to the method? How come you don't use any class member variables?



                      Doing it like you are, everything you allocate gets destroyed as soon as you leave the method. Wouldn't it make more sense to allocate the images to a class member so that other methods could access them?

                      Code:
                      public class MyClass
                      {
                        private Image[] images = new Image[2003];
                      
                        public MyClass()
                        {
                          // load images[...]
                        }
                      
                        public void SomeMethod()
                        {
                          // this method can now access images[...]
                        }
                      }
                      Your timer will tick every Interval, so why are you setting the interval again every time it ticks? You can set it once and it will not change until set elsewhere.

                      Lastly, think about what you want to do. You want to set an image to your background every X seconds such that the background will appear to animate. So why aren't you doing that? Your timer's tick method will go ahead and tick every 1000/30 seconds on and on, so what should you do in there to make your image animate?

                      What else do you need to keep track of what image is currently being shown? (Hint: See above... I've also said you need this in several posts above)

                      Keep at it, you're getting there. Just think about what you need and what you have.

                      You need your image changed every X seconds so that your background animates. You have a method that happens every X seconds.

                      Comment

                      • sword117
                        New Member
                        • Jun 2010
                        • 35

                        #12
                        is this right? i cant see the background..

                        Code:
                        public class Myclass
                                {
                                   private Image[] images = new Image[2003];
                        
                                    public Myclass()
                                    {
                                        
                                        for (int i = 1; i <= 440; i++)
                                        {
                                            images[i] = Image.FromFile(@"Images\1 (" + i + ").jpg");
                                        }
                                           
                                    }
                        
                                    public void MyClock()
                                    {
                                        Form1 form1 = new Form1();
                                        for (int i = 1; i <= 440; i++)
                                        {
                                            form1.BackgroundImage = images[i];
                                            form1.Refresh();
                                        }
                                    }

                        ive searched all over the web and i didnt find how to put timer count the this.background = images[];
                        im really desperate. please help =)

                        Comment

                        • sword117
                          New Member
                          • Jun 2010
                          • 35

                          #13
                          should i do this? http://msdn.microsoft.com/en-us/library/aa446493.aspx

                          Comment

                          • GaryTexmo
                            Recognized Expert Top Contributor
                            • Jul 2009
                            • 1501

                            #14
                            ive searched all over the web and i didnt find how to put timer count the this.background = images[];
                            im really desperate. please help =)
                            What's stopping you from doing this...

                            Code:
                            public class Blah : Form
                            {
                              private int m_currentFrame = 0;
                              private Image[] m_images = new Image[2003];
                            
                              ...
                            
                              public void Timer_Tick(...)
                              {
                                this.BackgroundImage = m_images[m_currentFrame];
                            
                                m_currentFrame++;
                                if (m_currentFrame > m_images.Length) m_currentFrame = 0;
                              }
                            }

                            Comment

                            • sword117
                              New Member
                              • Jun 2010
                              • 35

                              #15
                              thank you very much! youre a life saver!!!
                              thanks x 1 million!!!

                              best regards =)


                              heres the final code =)

                              Code:
                               private int m_countframe = 1;
                                      private Image[] m_images = new Image[2003];
                              
                                  private void timer1_Tick(object sender, EventArgs e)
                                          {
                                   
                                              
                                              timer1.Interval = 1000 / 30;
                              
                                              this.BackgroundImage = m_images[m_countframe];
                                              m_countframe++;
                                              m_images[m_countframe] = Image.FromFile(@"Images\1 (" + m_countframe + ").jpg");
                                              if (m_countframe > m_images.Length) m_countframe = 1;
                              }

                              Comment

                              Working...