如何让动画 gif 在 WPF 中工作? [英] How do I get an animated gif to work in WPF?
问题描述
我应该使用哪种控件类型 - Image
、MediaElement
等?
What control type should I use - Image
, MediaElement
, etc.?
推荐答案
我无法得到这个问题的最流行答案(Dario 以上)以正常工作.结果是奇怪的、断断续续的动画和奇怪的工件.到目前为止我找到的最佳解决方案:https://github.com/XamlAnimatedGif/WpfAnimatedGif
I couldn't get the most popular answer to this question (above by Dario) to work properly. The result was weird, choppy animation with weird artifacts. Best solution I have found so far: https://github.com/XamlAnimatedGif/WpfAnimatedGif
你可以用 NuGet 安装
You can install it with NuGet
PM>安装包 WpfAnimatedGif
并使用它,在要添加 gif 图像的窗口的新命名空间中使用它,如下所示
and to use it, at a new namespace to the Window where you want to add the gif image and use it as below
<Window x:Class="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" <!-- THIS NAMESPACE -->
Title="MainWindow" Height="350" Width="525">
<Grid>
<!-- EXAMPLE USAGE BELOW -->
<Image gif:ImageBehavior.AnimatedSource="Images/animated.gif" />
这个包真的很整洁,你可以设置一些像下面这样的属性
The package is really neat, you can set some attributes like below
<Image gif:ImageBehavior.RepeatBehavior="3x"
gif:ImageBehavior.AnimatedSource="Images/animated.gif" />
你也可以在你的代码中使用它:
and you can use it in your code as well:
var image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(fileName);
image.EndInit();
ImageBehavior.SetAnimatedSource(img, image);
Silverlight 支持
根据 josh2112 的评论,如果您想为 Silverlight 项目添加动画 GIF 支持,请使用 github.com/XamlAnimatedGif/XamlAnimatedGif
As per josh2112's comment if you want to add animated GIF support to your Silverlight project then use github.com/XamlAnimatedGif/XamlAnimatedGif
这篇关于如何让动画 gif 在 WPF 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!