WPF 开源gif控件WpfAnimatedGif的使用方法

本文介绍如何在WPF项目中使用WpfAnimatedGif控件播放GIF图片,包括基本引用设置、XAML界面配置、播放行为控制及代码级操作等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目中,需要用到播放gif图片,目前开源的gif控件推荐github上的WpfAnimatedGif,github源码地址为:
https://github.com/XamlAnimatedGif/WpfAnimatedGif
wiki 使用文档:
https://github.com/XamlAnimatedGif/WpfAnimatedGif/wiki
里面写的很详细,我这里挑主要的说明下:
1.引用,声明命名控件

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

2.在xaml界面上直接使用控件

<Image gif:ImageBehavior.AnimatedSource="Images/animated.gif" />

3.控制gif的播放行为

 <!-- Repeat 3 times -->
<Image gif:ImageBehavior.RepeatBehavior="3x"
       gif:ImageBehavior.AnimatedSource="Images/animated.gif" />

 <!-- Repeat for 10 seconds -->
<Image gif:ImageBehavior.RepeatBehavior="0:0:10"
       gif:ImageBehavior.AnimatedSource="Images/animated.gif" />

 <!-- Repeat forever -->
<Image gif:ImageBehavior.RepeatBehavior="Forever"
       gif:ImageBehavior.AnimatedSource="Images/animated.gif" />

4.vs设计模式预览gif

<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"
        gif:ImageBehavior.AnimateInDesignMode="True">

5.代码使用-更改图片

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

6.代码使用-控制播放

var controller = ImageBehavior.GetAnimationController(imageControl);
// Pause the animation
controller.Pause();

// Resume the animation (or restart it if it was completed)
controller.Play();

// Go to the last frame
controller.GotoFrame(controller.FrameCount - 1);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值