為 wpf 播放 gif 傷神不?

NuGet 包:WpfAnimatedGif。
今天介绍一个用于在 WPF 中显示动态 GIF 图片的库,可在 XAML 或代码中使用:WpfAnimatedGif。
简单易用:在 XAML 中,使用AnimatedSource附加属性设置需要显示的 gif 图片(替换Source属性):
<Window
x:Class="WpfAnimatedGif.Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gif="http://wpfanimatedgif.codeplex.com"
Title="MainWindow"
Height="350"
Width="525"
>
<Grid> <image gif:ImageBehavior.AnimatedSource="Images/animated.gif" /></Grid
></Window>

您还可以指定重复行为(默认为0x,这意味着它将使用来自 GIF 元数据的重复计数):
<image
gif:ImageBehavior.RepeatBehavior="3x"
gif:ImageBehavior.AnimatedSource="Images/animated.gif"
/>

當然,您也可以在代碼中設置 gif 圖片:
var image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(fileName);
image.EndInit();
ImageBehavior.SetAnimatedSource(img, image);

有关使用的更多详细信息,请参阅wiki。
特色
- 未增加新的控件,在 WPF 原生的
Image控件中添加附加属性即实现了 gif 图片动态加载功能 - 考慮實際幀持續時間
- 可以指定重複行為;如果未指定,則使用來自 gif 元數據的重複計數
- 動畫播放完成時可通知,可用於動畫完成後做一些特定的事
- 設計模式下的動畫預覽(必須明確啟用)
- 支持手動控制動畫(暫停/恢復/跳轉)