在WPF的Gif动画 [英] Animating Gif in WPF
问题描述
我使用的seprate库
和XAML代码在我的主要项目GIF动画效果这段代码:
I am using this code for gif Animating in seprate library and xaml code in my main project:
<controls:GifImage GifSource="/project;component/Images/my.gif" Stretch="None" />
的Gif动画效果(seprate文件):
Gif Animating (seprate file):
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];
}
/// <summary>
/// Defines whether the animation starts on it's own
/// </summary>
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
/// <summary>
/// Shows and starts the gif animation
/// </summary>
public void Show()
{
this.Visibility = Visibility.Visible;
this.StartAnimation();
}
/// <summary>
/// Hides and stops the gif animation
/// </summary>
public void Hide()
{
this.Visibility = Visibility.Collapsed;
this.StopAnimation();
}
/// <summary>
/// Starts the animation
/// </summary>
public void StartAnimation()
{
if (!_isInitialized)
this.Initialize();
BeginAnimation(FrameIndexProperty, _animation);
}
/// <summary>
/// Stops the animation
/// </summary>
public void StopAnimation()
{
BeginAnimation(FrameIndexProperty, null);
}
#endregion Public Instance Methods
}
但我得到的错误:
的URI前缀无法识别
我不知道为什么我得到错误。 ?可能有人帮助我,请
I am not sure why i get error. Could someone help me please ?
推荐答案
有一个更容易显示在WPF动态.gif方式 - 使用的MediaElement
There is a much easier way to show an animated .gif in wpf - use the MediaElement
例如:
<MediaElement x:Name="myGif" MediaEnded="myGif_MediaEnded" UnloadedBehavior="Manual" Source="file://C:\waiting.GIF" LoadedBehavior="Play" Stretch="None"/>
如果您希望.gif若要无限循环,但只指定了重复的数量有限。 gif文件,你可以连接 MediaEnded
键,只需重新启动动画(请务必将 UnloadedBehavior
到手动
):
If you want the .gif to loop endlessly but it only specifies a limited amount of repeats in the .gif file, you can hook MediaEnded
and just restart the animation (Be sure to set the UnloadedBehavior
to Manual
):
private void myGif_MediaEnded(object sender, RoutedEventArgs e)
{
myGif.Position = new TimeSpan(0, 0, 1);
myGif.Play();
}
这篇关于在WPF的Gif动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!