在WPF中动画Gif
我在seprate库中使用此代码进行gif Animating,并在我的主项目中使用xaml代码:
Gif Animating(分隔文件):
public class GifImage : Image { #region Memmbers private GifBitmapDecoder _gifDecoder; private Int32Animation _animation; private bool _isInitialized; #endregion Memmbers #region Properties private int FrameIndex { get { return (int)GetValue(FrameIndexProperty); } set { SetValue(FrameIndexProperty, value); } } private static readonly DependencyProperty FrameIndexProperty = DependencyProperty.Register("FrameIndex", typeof(int), typeof(GifImage), new FrameworkPropertyMetadata(0, new PropertyChangedCallback(ChangingFrameIndex))); private static void ChangingFrameIndex(DependencyObject obj, DependencyPropertyChangedEventArgs ev) { GifImage image = obj as GifImage; image.Source = image._gifDecoder.Frames[(int)ev.NewValue]; } /// /// Defines whether the animation starts on it's own /// public bool AutoStart { get { return (bool)GetValue(AutoStartProperty); } set { SetValue(AutoStartProperty, value); } } public static readonly DependencyProperty AutoStartProperty = DependencyProperty.Register("AutoStart", typeof(bool), typeof(GifImage), new UIPropertyMetadata(false, AutoStartPropertyChanged)); private static void AutoStartPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { if ((bool)e.NewValue) (sender as GifImage).StartAnimation(); } public string GifSource { get { return (string)GetValue(GifSourceProperty); } set { SetValue(GifSourceProperty, value); } } public static readonly DependencyProperty GifSourceProperty = DependencyProperty.Register("GifSource", typeof(string), typeof(GifImage), new UIPropertyMetadata(string.Empty, GifSourcePropertyChanged)); private static void GifSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { // CARLO 20100622: Reinitialize animation everytime image is changed (sender as GifImage).Initialize(); } #endregion Properties #region Private Instance Methods private void Initialize() { _gifDecoder = new GifBitmapDecoder(new Uri(String.Format("pack://application:,,,{0}", this.GifSource)), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); _animation = new Int32Animation(0, _gifDecoder.Frames.Count - 1, new Duration(new TimeSpan(0, 0, 0, _gifDecoder.Frames.Count / 10, (int)((_gifDecoder.Frames.Count / 10.0 - _gifDecoder.Frames.Count / 10) * 1000)))); _animation.RepeatBehavior = RepeatBehavior.Forever; this.Source = _gifDecoder.Frames[0]; _isInitialized = true; } #endregion Private Instance Methods #region Public Instance Methods /// /// Shows and starts the gif animation /// public void Show() { this.Visibility = Visibility.Visible; this.StartAnimation(); } /// /// Hides and stops the gif animation /// public void Hide() { this.Visibility = Visibility.Collapsed; this.StopAnimation(); } /// /// Starts the animation /// public void StartAnimation() { if (!_isInitialized) this.Initialize(); BeginAnimation(FrameIndexProperty, _animation); } /// /// Stops the animation /// public void StopAnimation() { BeginAnimation(FrameIndexProperty, null); } #endregion Public Instance Methods }
但我得到错误:
无法识别URI前缀。
我不知道为什么我会收到错误。 有人可以帮帮我吗?
在wpf中显示动画.gif有一种更简单的方法 – 使用MediaElement
例:
如果你想让.gif无限循环,但它只在.gif文件中指定了有限数量的重复,你可以挂钩MediaEnded
并重新启动动画(确保将UnloadedBehavior
设置为Manual
):
private void myGif_MediaEnded(object sender, RoutedEventArgs e) { myGif.Position = new TimeSpan(0, 0, 1); myGif.Play(); }
试试这个:
顺便说一句,我发现使用这种方式在wpf中播放gif可能会使一些gif图像变形,我想知道为什么……
我不能相信这一点,但这是一种只在XAML中执行此操作的方法。 我在ViewModel中添加了一个“IsBusy”属性,以在处理过程中显示/隐藏微调器。
这是作者解决方案的链接 。