How to create a video background using frames and timer?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #16
    Glad you got it working :)

    I would still preload all your images in the constructor or form_load event first so you're not doing a disk read every cycle though. A disk read is very slow. If they're all precached in the array, you're just changing the reference to existing memory... ie, fast ;)

    Comment

    • sword117
      New Member
      • Jun 2010
      • 35

      #17
      Originally posted by GaryTexmo
      Glad you got it working :)

      I would still preload all your images in the constructor or form_load event first so you're not doing a disk read every cycle though. A disk read is very slow. If they're all precached in the array, you're just changing the reference to existing memory... ie, fast ;)
      OMG XD how do i preload the images in the form_load? xD

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #18
        Well, you were already doing it in the constructor (see your post... #12), I'm not sure why you took it out. I don't see any reason why you couldn't just move that code to a form_load event instead if you wanted to do it there...

        Comment

        • sword117
          New Member
          • Jun 2010
          • 35

          #19
          Originally posted by GaryTexmo
          Well, you were already doing it in the constructor (see your post... #12), I'm not sure why you took it out. I don't see any reason why you couldn't just move that code to a form_load event instead if you wanted to do it there...
          so i did this but it doesnt work. i copy the code from post 12 but the program doesnt even open..
          how should i put the code?

          Code:
              private int CountFrames = 1;
                  private Image[] Frames = new Image[5191];
                  private void Form1_Load(object sender, EventArgs e)
                  {
                      Text = "My Clock";
          
                      Frames[CountFrames] = Image.FromFile(@"Frames\1 (" + CountFrames + ").jpg");
                          
                  }
          
          
                  private void timer1_Tick(object sender, EventArgs e)
                  {
                      timer1.Interval = 1000 / 30;
                      CountFrames++;
                      
                      this.BackgroundImage = Frames[CountFrames];
                      if (CountFrames > Frames.Length) CountFrames = 1;
                      
          }

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #20
            That's not the code from the constructor in post 12 at all. Look again.

            Comment

            • sword117
              New Member
              • Jun 2010
              • 35

              #21
              okay i got this, but it becomes very slow and the form takes a lot of time to appear, and the timer1.interval doesnt seem to apply to the timer...

              Code:
              public partial class Form1 : Form
                  {
              
              
                      private int CountFrames = 1;
                      private Image[] Frames = new Image[5191];
              
                     
                      public Form1()
                      {
                          InitializeComponent();
              
                          for (int i = 1; i <= 5190; i++)
                          {
                              Frames[i] = Image.FromFile(@"Frames\1 (" + i + ").jpg");
                          }
                          
                      }
              
                      
                      private void Form1_Load(object sender, EventArgs e)
                      {
                          Text = "My Clock";
                      }
              
              
                      private void timer1_Tick(object sender, EventArgs e)
                      {
                          timer1.Interval = 1000 / 30;
                          CountFrames++;
                          
                          this.BackgroundImage = Frames[CountFrames];
                          if (CountFrames > Frames.Length) CountFrames = 1;
                          progressBar1.Value = CountFrames;
                          label1.Text = CountFrames + "%";
                      }
              
                  }

              Comment

              • sword117
                New Member
                • Jun 2010
                • 35

                #22
                and when i execute the program it has 3.279.108k on task manager..

                theres any solucion possible for reducing the memory?

                Comment

                • sword117
                  New Member
                  • Jun 2010
                  • 35

                  #23
                  Originally posted by GaryTexmo
                  That's not the code from the constructor in post 12 at all. Look again.
                  okay i got this, but it becomes very slow and the form takes a lot of time to appear, and the timer1.interval doesnt seem to apply to the timer.

                  and still when i execute the program it has 3.279.108k on task manager ,theres any solucion possible for reducing the memory? =)

                  Code:
                   public partial class Form1 : Form
                          {
                       
                       
                              private int CountFrames = 1;
                              private Image[] Frames = new Image[5191];
                       
                       
                             public Form1()
                            {
                                 InitializeComponent();
                      
                                 for (int i = 1; i <= 5190; i++)
                                 {
                                     Frames[i] = Image.FromFile(@"Frames\1 (" + i + ").jpg");
                                 }
                      
                             }
                      
                      
                             private void Form1_Load(object sender, EventArgs e)
                             {
                                 Text = "My Clock";
                             }
                      
                      
                             private void timer1_Tick(object sender, EventArgs e)
                             {
                                 timer1.Interval = 1000 / 30;
                                 CountFrames++;
                      
                                 this.BackgroundImage = Frames[CountFrames];
                                 if (CountFrames > Frames.Length) CountFrames = 1;
                                 progressBar1.Value = CountFrames;
                                 label1.Text = CountFrames + "%";
                             }
                      
                         }

                  Comment

                  • GaryTexmo
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1501

                    #24
                    It's taking a lot of time to appear 'cause you're loading roughly 5200 images from the hard drive into memory. There's going to be a delay, the plus side is that now front-loaded and happens once per image. You're no longer loading the image from disk over and over every time you change the frame.

                    If you're concerned about this, you could always use a background worker to "buffer" the files and start playing as you're loading them from the disk. I don't mean to offend, but given what I've seen you do from this thread I think this might be a bit above your current abilities. Unless you have some pressing need to avoid the load delay at the start, just leave it. Otherwise you'll have to research background workers and threading, which you're free to do but it'll be some work :)

                    As for the timer not happening in the correct interval, your code looks right. Technically you don't need to set the timer interval over and over every timer tick (once in the constructor or designer is sufficient).

                    I actually plugged your code in and it's working fine. Did you create a new timer and forget to link up the tick event and/or enable it?

                    Comment

                    • sword117
                      New Member
                      • Jun 2010
                      • 35

                      #25
                      Originally posted by GaryTexmo
                      It's taking a lot of time to appear 'cause you're loading roughly 5200 images from the hard drive into memory. There's going to be a delay, the plus side is that now front-loaded and happens once per image. You're no longer loading the image from disk over and over every time you change the frame.

                      If you're concerned about this, you could always use a background worker to "buffer" the files and start playing as you're loading them from the disk. I don't mean to offend, but given what I've seen you do from this thread I think this might be a bit above your current abilities. Unless you have some pressing need to avoid the load delay at the start, just leave it. Otherwise you'll have to research background workers and threading, which you're free to do but it'll be some work :)

                      As for the timer not happening in the correct interval, your code looks right. Technically you don't need to set the timer interval over and over every timer tick (once in the constructor or designer is sufficient).

                      I actually plugged your code in and it's working fine. Did you create a new timer and forget to link up the tick event and/or enable it?
                      well i only started c# about 8 months in the school, and my teacher doesnt teach us very much, so youre right, but im trying to learn all that i can, because i want to be a good programmer and thats my way of the ninja xD

                      but no i didnt create a new timer, i will search the web for the background worker. =) then if i dont know what to do im gonna create another post. thanks to you i learned new things and you have my thanks and best regards =)

                      Comment

                      • GaryTexmo
                        Recognized Expert Top Contributor
                        • Jul 2009
                        • 1501

                        #26
                        Yea, don't take that to mean you shouldn't try, just that it may not be good to try just yet. Build your skills, then take a crack at it after. Sometimes it's best not to overwhelm yourself by trying to do too much at once.

                        Of course, if you're in for it you're in for it ;)

                        Regarding the timer, are you sure you didn't disable it somehow? I copy/pasted your code into a new project. I commented out the image stuff and just updated a progress bar/label with the counter. It worked just fine for me once I hooked the timer up to the timer_tick method. Make sure you check the events pane in the designer view for your timer. Also make sure enabled is set to true.

                        If your timer isn't working and everything is hooked up properly, then I'm not sure what the problem is. If you're still having trouble with it, post your designer file along with your source file and I can look.

                        Glad you learned something, good luck with the buffering if you do dive in :)

                        Comment

                        • sword117
                          New Member
                          • Jun 2010
                          • 35

                          #27
                          Originally posted by GaryTexmo
                          Yea, don't take that to mean you shouldn't try, just that it may not be good to try just yet. Build your skills, then take a crack at it after. Sometimes it's best not to overwhelm yourself by trying to do too much at once.

                          Of course, if you're in for it you're in for it ;)

                          Regarding the timer, are you sure you didn't disable it somehow? I copy/pasted your code into a new project. I commented out the image stuff and just updated a progress bar/label with the counter. It worked just fine for me once I hooked the timer up to the timer_tick method. Make sure you check the events pane in the designer view for your timer. Also make sure enabled is set to true.

                          If your timer isn't working and everything is hooked up properly, then I'm not sure what the problem is. If you're still having trouble with it, post your designer file along with your source file and I can look.

                          Glad you learned something, good luck with the buffering if you do dive in :)
                          sorry for the delay, but finally here is my project =)

                          Comment

                          • GaryTexmo
                            Recognized Expert Top Contributor
                            • Jul 2009
                            • 1501

                            #28
                            Ok, so I ran your code and the timer actually worked just fine for me. Not sure what problem you're having.

                            The problem I had was that it does indeed take a while to load and a ton of memory. I actually couldn't load the whole thing because my system doesn't have enough memory, which seems quite odd 'cause those pictures are only 34kb and 1000 images brings me up over 1gb.

                            I'm guessing there's a whole bunch of overhead associated with the image class, which is rather unfortunate actually.

                            What I also don't get is when I do this, as the program runs, the memory footprint actually decreases over time. Yea, I have no idea.

                            Actually, a bit of googling turned this up:


                            I guess that when they're loaded into memory, they get uncompressed and are turned into bitmaps, hence the huge memory consumption. I don't get why the memory footprint decreases over time, but w/e.

                            Anyway, it turns out that I was wrong here. Apparently it's better to load it straight from the hard drive when needed... go figure! That's what I get for trying to be all efficient and whatnot.

                            You can remove the Image array and just load the image from the counter as you require it. My apologies for steering you in the wrong direction here!

                            Comment

                            • sword117
                              New Member
                              • Jun 2010
                              • 35

                              #29
                              Originally posted by GaryTexmo
                              Ok, so I ran your code and the timer actually worked just fine for me. Not sure what problem you're having.

                              The problem I had was that it does indeed take a while to load and a ton of memory. I actually couldn't load the whole thing because my system doesn't have enough memory, which seems quite odd 'cause those pictures are only 34kb and 1000 images brings me up over 1gb.

                              I'm guessing there's a whole bunch of overhead associated with the image class, which is rather unfortunate actually.

                              What I also don't get is when I do this, as the program runs, the memory footprint actually decreases over time. Yea, I have no idea.

                              Actually, a bit of googling turned this up:


                              I guess that when they're loaded into memory, they get uncompressed and are turned into bitmaps, hence the huge memory consumption. I don't get why the memory footprint decreases over time, but w/e.

                              Anyway, it turns out that I was wrong here. Apparently it's better to load it straight from the hard drive when needed... go figure! That's what I get for trying to be all efficient and whatnot.

                              You can remove the Image array and just load the image from the counter as you require it. My apologies for steering you in the wrong direction here!
                              lol you dont need apologies, im very grateful for your help =)

                              Comment

                              • sword117
                                New Member
                                • Jun 2010
                                • 35

                                #30
                                Originally posted by GaryTexmo
                                Ok, so I ran your code and the timer actually worked just fine for me. Not sure what problem you're having.

                                The problem I had was that it does indeed take a while to load and a ton of memory. I actually couldn't load the whole thing because my system doesn't have enough memory, which seems quite odd 'cause those pictures are only 34kb and 1000 images brings me up over 1gb.

                                I'm guessing there's a whole bunch of overhead associated with the image class, which is rather unfortunate actually.

                                What I also don't get is when I do this, as the program runs, the memory footprint actually decreases over time. Yea, I have no idea.

                                Actually, a bit of googling turned this up:


                                I guess that when they're loaded into memory, they get uncompressed and are turned into bitmaps, hence the huge memory consumption. I don't get why the memory footprint decreases over time, but w/e.

                                Anyway, it turns out that I was wrong here. Apparently it's better to load it straight from the hard drive when needed... go figure! That's what I get for trying to be all efficient and whatnot.

                                You can remove the Image array and just load the image from the counter as you require it. My apologies for steering you in the wrong direction here!
                                i got another problem that i didnt notice... when the image reaches the end it gives an error that the index was outside of the bounds of the array..

                                what can i do? =)

                                the current code is this(its the same that you got).
                                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;
                                            m_countframe++;
                                            m_images[m_countframe] = Image.FromFile(@"Images\1 (" + m_countframe + ").jpg");
                                            this.BackgroundImage = m_images[m_countframe];
                                            if (m_countframe > m_images.Length) m_countframe = 1;
                                
                                      //progressBar1 Value 
                                            progressBar1.Value = m_countframe;
                                      //label1 Text
                                            label2.Text = m_countframe + "%";
                                
                                      //time
                                            double hour = DateTime.Now.Hour;
                                            double min = DateTime.Now.Minute;
                                            double sec = DateTime.Now.Second;
                                
                                            if (hour <= 13)
                                            {
                                                label1.Text = hour + ":" + min + ":" + sec + "am";
                                            }
                                            if (hour >= 13)
                                            {
                                                label1.Text = hour + ":" + min + ":" + sec + "pm";
                                            }
                                
                                
                                
                                
                                        }

                                Comment

                                Working...