Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. HowTo Avoid Flicker

HowTo Avoid Flicker

Scheduled Pinned Locked Moved C#
graphicsquestionannouncement
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zaegra
    wrote on last edited by
    #1

    Heya, I have the following code that stretches a control to its parent size in a given number of steps. However, the panel1.update() method causes the screen to flicker. Is there a workaround for this, so that the animation will run smooth?

    public void Animate(Control Ctrl, int Steps)
    {
    Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
    Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
    int ParentWidth = (int)Ctrl.Parent.Width;
    int ParentHeight = (int)Ctrl.Parent.Height;
    int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
    int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
    panel1.BackgroundImage = OriginalView;
    Ctrl.Visible = false;

            for (int i = 0; i < Steps; i++)
            {
                Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth));
                panel1.BackgroundImage = NewRender;
                panel1.Update(); 
            }
            GC.Collect();
            Ctrl.Visible = true;
            panel1.BackgroundImage = null;
            panel1.Update();
            
    
        }
    

    Thanks in advance, Zaegra

    Motivation is the key to software development.

    L M E 4 Replies Last reply
    0
    • Z Zaegra

      Heya, I have the following code that stretches a control to its parent size in a given number of steps. However, the panel1.update() method causes the screen to flicker. Is there a workaround for this, so that the animation will run smooth?

      public void Animate(Control Ctrl, int Steps)
      {
      Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
      Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
      int ParentWidth = (int)Ctrl.Parent.Width;
      int ParentHeight = (int)Ctrl.Parent.Height;
      int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
      int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
      panel1.BackgroundImage = OriginalView;
      Ctrl.Visible = false;

              for (int i = 0; i < Steps; i++)
              {
                  Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth));
                  panel1.BackgroundImage = NewRender;
                  panel1.Update(); 
              }
              GC.Collect();
              Ctrl.Visible = true;
              panel1.BackgroundImage = null;
              panel1.Update();
              
      
          }
      

      Thanks in advance, Zaegra

      Motivation is the key to software development.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Enable Double Buffering on the control.

      1 Reply Last reply
      0
      • Z Zaegra

        Heya, I have the following code that stretches a control to its parent size in a given number of steps. However, the panel1.update() method causes the screen to flicker. Is there a workaround for this, so that the animation will run smooth?

        public void Animate(Control Ctrl, int Steps)
        {
        Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
        Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
        int ParentWidth = (int)Ctrl.Parent.Width;
        int ParentHeight = (int)Ctrl.Parent.Height;
        int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
        int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
        panel1.BackgroundImage = OriginalView;
        Ctrl.Visible = false;

                for (int i = 0; i < Steps; i++)
                {
                    Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth));
                    panel1.BackgroundImage = NewRender;
                    panel1.Update(); 
                }
                GC.Collect();
                Ctrl.Visible = true;
                panel1.BackgroundImage = null;
                panel1.Update();
                
        
            }
        

        Thanks in advance, Zaegra

        Motivation is the key to software development.

        M Offline
        M Offline
        musefan
        wrote on last edited by
        #3

        What is the point of the for loop, there is no pause in between updates, thus you will get little visual effect from it. This will likely be one reason for your flickering too. I would suggest using a timer (of some sort) and use the paint event of the panel to do your drawing. That way you can calculate any resize values when you start the animation, and apply the values with each tick of the timer along with a call to panel.Invalidate();

        Life goes very fast. Tomorrow, today is already yesterday.

        1 Reply Last reply
        0
        • Z Zaegra

          Heya, I have the following code that stretches a control to its parent size in a given number of steps. However, the panel1.update() method causes the screen to flicker. Is there a workaround for this, so that the animation will run smooth?

          public void Animate(Control Ctrl, int Steps)
          {
          Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
          Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
          int ParentWidth = (int)Ctrl.Parent.Width;
          int ParentHeight = (int)Ctrl.Parent.Height;
          int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
          int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
          panel1.BackgroundImage = OriginalView;
          Ctrl.Visible = false;

                  for (int i = 0; i < Steps; i++)
                  {
                      Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth));
                      panel1.BackgroundImage = NewRender;
                      panel1.Update(); 
                  }
                  GC.Collect();
                  Ctrl.Visible = true;
                  panel1.BackgroundImage = null;
                  panel1.Update();
                  
          
              }
          

          Thanks in advance, Zaegra

          Motivation is the key to software development.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Instead of visible = false/visible = true, use BeginUpdate() and EndUpdate() methods.

          1 Reply Last reply
          0
          • Z Zaegra

            Heya, I have the following code that stretches a control to its parent size in a given number of steps. However, the panel1.update() method causes the screen to flicker. Is there a workaround for this, so that the animation will run smooth?

            public void Animate(Control Ctrl, int Steps)
            {
            Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
            Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
            int ParentWidth = (int)Ctrl.Parent.Width;
            int ParentHeight = (int)Ctrl.Parent.Height;
            int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
            int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
            panel1.BackgroundImage = OriginalView;
            Ctrl.Visible = false;

                    for (int i = 0; i < Steps; i++)
                    {
                        Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth));
                        panel1.BackgroundImage = NewRender;
                        panel1.Update(); 
                    }
                    GC.Collect();
                    Ctrl.Visible = true;
                    panel1.BackgroundImage = null;
                    panel1.Update();
                    
            
                }
            

            Thanks in advance, Zaegra

            Motivation is the key to software development.

            E Offline
            E Offline
            ely_bob
            wrote on last edited by
            #5

            the biggest issue I see is the panel.BackgroundImage = null; delete that... what you are getting by setting the null is : image no image image no image hence the flicker. all you need to do is change the image. when you do, it will throw the Paint command automatically(if memory serves if not throw it manually). What I usually do(manual double buffering)

            bitmap bmp0,bmp1;

            control.Image = bmp0;
            bmp1 = bmp0;
            (modify bmp1}
            bmp0 = bmp1;
            control.Image = bmp0;

            no flicker!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups