All pastes #2090327 Raw Edit

Anonymous

public text v1 · immutable
#2090327 ·published 2011-10-15 17:01 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Drawing;

namespace AnimationTime
{
    class AnimatedPictureFrame :System.Windows.Forms.Control
    {
        private const int AnimationInterval = 100;

        private List<AnimatedPicture> Images = new List<AnimatedPicture>();
        private System.Threading.Timer AnimationTimer;

        Graphics g = null;

        public AnimatedPictureFrame()
        {
            AnimationTimer = new Timer(new TimerCallback(AnimationTick), null, Timeout.Infinite, Timeout.Infinite);
        }

        public void Set(List<AnimatedPicture> Images)
        {
            // Store the images
            this.Images = Images;

            // Animate the images
            AnimationTimer.Change(AnimationInterval, AnimationInterval);
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
        {
            // Do default, then draw over top
            base.OnPaint(pe);

            g = pe.Graphics;
            
            //foreach(AnimatedPicture image in Images)
            //{
            //    // Draw the current frame
            //    g.DrawImage(image.ImageToAnimate, image.SizeRect, image.CurrentCell, GraphicsUnit.Pixel);

            //    // Prepare for the next render
            //    image.Update();
            //}
        }

        protected void DoPaint()
        {
            g.Clear(Color.DimGray);

            foreach (AnimatedPicture image in Images)
            {
                // Draw the current frame
                g.DrawImage(image.ImageToAnimate, image.SizeRect, image.CurrentCell, GraphicsUnit.Pixel);

                // Prepare for the next render
                image.Update();
            }
        }

        public void Clear()
        {
            // Stop Animating
            AnimationTimer.Change(Timeout.Infinite, Timeout.Infinite);

            foreach (AnimatedPicture image in Images)
            {
                image.Dispose();
            }

            Images.Clear();
        }

        protected void AnimationTick(object state)
        {
            // ReDraw
            //this.Invalidate();
            DoPaint();
        }
    }
}