C#中的GIF动画CPU消耗问题!

时间:2010-08-27 07:14:06

标签: c# wpf animation cpu-usage gif

我制作了一个GIF动画用户控件,用于加载gif动画,并在我的项目中创建了大约30个动画,每个动画都有动画GIF。问题是当我检查我的CPU使用率大约是70%+ !!!我确定这有问题! 请帮我。以下是GIF Animator控件的代码:

public class AnimatedImage : System.Windows.Controls.Image
{
    private BitmapSource[] _BitmapSources = null;
    private int _nCurrentFrame=0;


    private bool _bIsAnimating=false;

    public bool IsAnimating
    {
        get { return _bIsAnimating; }
    }

    static AnimatedImage()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(AnimatedImage), new FrameworkPropertyMetadata(typeof(AnimatedImage)));
    }
    public Bitmap AnimatedBitmap
    {
        get { return (Bitmap)GetValue(AnimatedBitmapProperty); }
        set { StopAnimate(); SetValue(AnimatedBitmapProperty, value); }
    }

    /// <summary>
    /// Identifies the Value dependency property.
    /// </summary>
    public static readonly DependencyProperty AnimatedBitmapProperty =
        DependencyProperty.Register(
            "AnimatedBitmap", typeof(Bitmap), typeof(AnimatedImage),
            new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnAnimatedBitmapChanged)));

    private static void OnAnimatedBitmapChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        AnimatedImage control = (AnimatedImage)obj;

        control.UpdateAnimatedBitmap();

        RoutedPropertyChangedEventArgs<Bitmap> e = new RoutedPropertyChangedEventArgs<Bitmap>(
            (Bitmap)args.OldValue, (Bitmap)args.NewValue, AnimatedBitmapChangedEvent);
        control.OnAnimatedBitmapChanged(e);
    }

    /// <summary>
    /// Identifies the ValueChanged routed event.
    /// </summary>
    public static readonly RoutedEvent AnimatedBitmapChangedEvent = EventManager.RegisterRoutedEvent(
        "AnimatedBitmapChanged", RoutingStrategy.Bubble,
        typeof(RoutedPropertyChangedEventHandler<Bitmap>), typeof(AnimatedImage));

    /// <summary>
    /// Occurs when the Value property changes.
    /// </summary>
    public event RoutedPropertyChangedEventHandler<Bitmap> AnimatedBitmapChanged
    {
        add { AddHandler(AnimatedBitmapChangedEvent, value); }
        remove { RemoveHandler(AnimatedBitmapChangedEvent, value); }
    }

     /// <summary>
     /// Raises the ValueChanged event.
     /// </summary>
    /// <param name="args">Arguments associated with the ValueChanged event.</param>
    protected virtual void OnAnimatedBitmapChanged(RoutedPropertyChangedEventArgs<Bitmap> args)
    {
        RaiseEvent(args);
      }
    private void UpdateAnimatedBitmap()
    {                       
        int nTimeFrames = AnimatedBitmap.GetFrameCount(System.Drawing.Imaging.FrameDimension.Time);
        _nCurrentFrame = 0;
        if (nTimeFrames > 0)
        {

            _BitmapSources = new BitmapSource[nTimeFrames];

            for (int i = 0; i < nTimeFrames; i++)
            {

                AnimatedBitmap.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Time, i);
                Bitmap bitmap = new Bitmap(AnimatedBitmap);
                bitmap.MakeTransparent();

                _BitmapSources[i] = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    bitmap.GetHbitmap(),
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
            }
            StartAnimate();
        }
    }
    private delegate void VoidDelegate();

    private void OnFrameChanged(object o, EventArgs e)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Render, new VoidDelegate(delegate { ChangeSource(); }));

    }
    void ChangeSource()
    {            
        Source = _BitmapSources[_nCurrentFrame++];
        _nCurrentFrame = _nCurrentFrame % _BitmapSources.Length;
        ImageAnimator.UpdateFrames();

    }

    public void StopAnimate()
    {
        if (_bIsAnimating)
        {
             ImageAnimator.StopAnimate(AnimatedBitmap, new EventHandler(this.OnFrameChanged));
            _bIsAnimating = false;
        }
    }

    public void StartAnimate()
    {
        if (!_bIsAnimating)
        {

            ImageAnimator.Animate(AnimatedBitmap, new EventHandler(this.OnFrameChanged));
            _bIsAnimating = true;
        }
    }

}

}

1 个答案:

答案 0 :(得分:1)

this one一个机会。我们已经在你的应用程序中使用它,我没有注意到任何奇怪的内存消耗。