WPF: Playing GIF is so frustrating!

WPF: Playing GIF is so frustrating!

Today we introduce a library for displaying dynamic GIF images in WPF, which can be used in XAML or code: `WpfAnimatedGif`.

最后更新 7/2/2021 10:12 PM
沙漠尽头的狼
预计阅读 2 分钟
分类
WPF
专题
WPF Open Source Project
标签
.NET WPF WPF Open Source Project open source GIF

Does playing GIF for WPF hurt?

WpfAnimatedGif

仓库地址:https://github.com/XamlAnimatedGif/WpfAnimatedGif

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"
/>

Of course, you can also set gif images in your code:

var image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(fileName);
image.EndInit();
ImageBehavior.SetAnimatedSource(img, image);

有关使用的更多详细信息,请参阅wiki

characteristics

  • 未增加新的控件,在 WPF 原生的Image控件中添加附加属性即实现了 gif 图片动态加载功能
  • Consider actual frame duration
  • Repeat behavior can be specified; if not, repeat counting from GIF metadata is used
  • Notify when the animation is completed, and can be used to do some specific things after the animation is completed
  • Animation preview in design mode (must be explicitly enabled)
  • Support manual animation control (pause/resume/jump)
Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 9/13/2025

Migration from WPF to Avalonia series: Why I have to migrate WPF programs to Avalonia

In the past few years, our host computer software has been mainly developed using WPF and WinForm. These technologies are really easy to use on the Windows platform, and they have also accompanied us through the stage of small-scale trial production to today's large-scale delivery. However, with the development of business and changes in customer needs, the single Windows technology stack has gradually become a hurdle that we must overcome.

继续阅读
同分类 / 同标签 1/26/2025

WPF internationalizes with custom XML files

This article describes in detail the methods of using custom XML files to achieve internationalization in WPF programs, including installing the necessary NuGet package, dynamically obtaining language lists, dynamically switching languages, using translation strings in code and xaml interfaces, etc. It also provides source code links to help developers easily internationalize WPF applications.

继续阅读