Lenguaje: C#
Desarrollado para: Aplicación de Consola C#
Clase que permite utilizar imagenes GIF animadas en aplicaciones de escritorio con WPF
- En el Summary de la misma clase aparece la descripción de como utilizarla, invocando un control en el archivo XAML.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace Miro.Utils
{
///
/// Clase para utilizar imagenes GIF en WPF
/// Forma de utilizar:
/// * Primero indicar el namespace en el archivo XAML
/// Ej.:xmlns:local="clr-namespace:WpfApplication1"
/// * Segundo invocar el control del tipo indicado anteriormente (local),
/// indicando en la propiedad GifSource la ruta de la imagen a utilizar
/// Ej.:""
///
public class GifImageUtils : Image
{
private GifBitmapDecoder _decoder;
private Int32Animation _animation;
#region FrameIndex
public static readonly DependencyProperty FrameIndexProperty = DependencyProperty.Register("FrameIndex", typeof(int), typeof(GifImageUtils), new UIPropertyMetadata(0, new PropertyChangedCallback(ChangingFrameIndex)));
public int FrameIndex
{
get { return (int)GetValue(FrameIndexProperty); }
set { SetValue(FrameIndexProperty, value); }
}
private static void ChangingFrameIndex(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
GifImageUtils gifImage = obj as GifImageUtils;
gifImage.ChangeFrameIndex((int)ev.NewValue);
}
private void ChangeFrameIndex(int index)
{
Source = _decoder.Frames[index];
}
#endregion
#region GifSource
public static readonly DependencyProperty GifSourceProperty = DependencyProperty.Register("GifSource", typeof(string), typeof(GifImageUtils), new UIPropertyMetadata(String.Empty, new PropertyChangedCallback(GifSourceChanged)));
public string GifSource
{
get { return (string)GetValue(GifSourceProperty); }
set { SetValue(GifSourceProperty, value); }
}
private static void GifSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs ev)
{
GifImageUtils gifImage = obj as GifImageUtils;
gifImage.GifSourceChanged(ev.NewValue.ToString());
}
private void GifSourceChanged(string newSource)
{
if (_animation != null)
BeginAnimation(FrameIndexProperty, null);
_decoder = new GifBitmapDecoder(new Uri(newSource), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
Source = _decoder.Frames[0];
int count = _decoder.Frames.Count;
_animation = new Int32Animation(0, count - 1, new Duration(new TimeSpan(0, 0, 0, count / 10, (int)((count / 10.0 - count / 10) * 1000))));
_animation.RepeatBehavior = RepeatBehavior.Forever;
BeginAnimation(FrameIndexProperty, _animation);
}
#endregion
}
}