获取GIF以在WPF中播放(带有GifImage类) [英] Get GIF to play in WPF (with GifImage Class)

查看:266
本文介绍了获取GIF以在WPF中播放(带有GifImage类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在这里找到的一段代码: 如何获取动画gif以便在其中工作WPF?

Following is a piece of code I found here: 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;
        }
    }
}

它应该允许我将GifImage用作控件.但是

It supposed to allow me to use GifImage as a Control. But

  1. 如何在XAML中使用它?
  2. 如何设置来源?我尝试了各种组合,但没有用,也没有文档:

GifImage gifImg = new GifImage(new Uri("/application; component/Images/indicator.gif");

GifImage gifImg = new GifImage(new Uri("/application;component/Images/indicator.gif");

不起作用,异常告诉我错误的路径,但我只是找不到正确的路径.帮助.

doesn't work and exception tells me a wrong path but I just can't get the right path. Help..

推荐答案

我对上面的代码做了一些修改,似乎可以正常工作.

I modified the above code a little and it seems to work.

Xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <local:GifImage GifSource="c:\data\test.gif" />
    </Grid>
</Window>

代码:

using System;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;


namespace WpfApplication1
{
    public class GifImage : Image
    {
        private GifBitmapDecoder _decoder;
        private Int32Animation _animation;

        #region FrameIndex

        public static readonly DependencyProperty FrameIndexProperty = DependencyProperty.Register("FrameIndex", typeof(int), typeof(GifImage), 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)
        {
            GifImage gifImage = obj as GifImage;
            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(GifImage), 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)
        {
            GifImage gifImage = obj as GifImage;
            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
    }
}

这篇关于获取GIF以在WPF中播放(带有GifImage类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆