為 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 中繼資料的重複計數
- 動畫播放完成時可通知,可用於動畫完成後做一些特定的事
- 設計模式下的動畫預覽(必須明確啟用)
- 支援手動控制動畫(暫停/恢復/跳轉)