2010-08-27 4 views
1

J'ai créé un contrôle d'utilisateur GIF Animation qui charge des animations gif et j'en ai créé environ 30 dans mon projet, chacune animant un GIF. Le problème est que lorsque je vérifie l'utilisation de mon processeur, il est d'environ 70% + !!! Je suis sûr que quelque chose ne va pas avec ça! S'il vous plaît aidez-moi. Voici le code de ce contrôle GIF Animator:GIF animation Problème de consommation CPU en C#!

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; 
     } 
    } 

} 

}

Répondre

1

Donnez this one un coup de feu. Nous l'avons utilisé dans notre application et je n'ai pas remarqué de consommation de mémoire bizarre.

+0

Mon problème n'est pas la consommation de mémoire, il consomme l'utilisation du processeur! – Moh

+0

Ca a marché ... merci Carlo :-) – Moh

+0

@MohammadAliannejadi On dirait que le site est mort, Pourriez-vous partager le code? Je veux de l'animation avec moins d'utilisation du CPU aussi. Merci – qakmak