Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > WPF и Silverlight > Как анимировать gif картинку в xaml?


Автор: xXxSataNxXx 13.12.2010, 23:43
Как можно настроить переключение фреймов в изображении gif с помощью xaml?
Или вообще, как анимировать gif изображение?

Автор: Kefir 8.1.2011, 02:02
Скопипастил http://stackoverflow.com/questions/210922/how-do-i-get-an-animated-gif-to-work-in-wpf:
Код

class GifImage : Image {

    public int FrameIndex {
        get { return (int)GetValue(FrameIndexProperty); }
        set { SetValue(FrameIndexProperty, value); }
    }

    public static readonly DependencyProperty FrameIndexProperty =
        DependencyProperty.Register("FrameIndex", typeof(int), typeof(GifImage), new UIPropertyMetadata(0, new PropertyChangedCallback(ChangingFrameIndex)));

    static void ChangingFrameIndex(DependencyObject obj, DependencyPropertyChangedEventArgs ev) {
        GifImage ob = obj as GifImage;
        ob.Source = ob.gf.Frames[(int)ev.NewValue];
        ob.InvalidateVisual();
    }
    GifBitmapDecoder gf;
    Int32Animation anim ;            
    public GifImage(Uri uri) {
        gf = new GifBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        anim = new Int32Animation(0, gf.Frames.Count - 1, new Duration(new TimeSpan(0,0, 0,gf.Frames.Count/10,(int)((gf.Frames.Count/10.0-gf.Frames.Count/10)*1000))));
        anim.RepeatBehavior = RepeatBehavior.Forever;
        Source = gf.Frames[0];
    }
    bool animationIsWorking = false;
    protected override void OnRender(DrawingContext dc) {
        base.OnRender(dc);
        if (!animationIsWorking) {
            BeginAnimation(FrameIndexProperty, anim);
            animationIsWorking = true;
        }
    }
}

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)