WPF 如何显示gif

最近碰到了要显示表情的需求,而表情刚好是gif的图片。

于是用了Image试了下,发现不行,只会显示第一帧,然后上网查了下资料,大致有这么几种方法,都可以实现。

第一种:

使用Winfrom里面的picturebox,缺点是要引用几个winfrom的dll

 

第二种:

用wpf的mediaelement控件,这控件本身是用来显示视频的,但是可以拿来放gif,

这种方式有一个局限就是图片路径必须是绝对路径

 <MediaElement Source="file://C:\129.gif" />

并且你还需要设置让他循环播放

<MediaElement Source="file://C:\129.gif" MediaEnded="MediaElement_MediaEnded"/>

  private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
  {
      ((MediaElement)sender).Position=((MediaElement)sender).Position.Add(TimeSpan.FromMilliseconds(1));
  }

而且我发现这种方法在win7以上的系统中才能使用,在XP系统下就失效了,所以果断放弃

 

 


第三种:

大致的思路是:使用GifBitmapDecoder类,其可以将动态GIF分解成很多帧并保存在一个列表中,每一帧为一个BitmapFrame类型的对象,其父类为BitmapSource,可以将每一帧赋值给一个Image控件的Source属性,这样可以得到针对GIF各帧的Image系列。

 

第四种:就是目前我在用的方法,重写下Wpf的Image控件

首先新建一个类继承自Image

 

public class GifImage : System.Windows.Controls.Image
{
/// <summary>
/// gif动画的System.Drawing.Bitmap
/// </summary>
private Bitmap gifBitmap;

/// <summary>
/// 用于显示每一帧的BitmapSource
/// </summary>
private BitmapSource bitmapSource;

public GifImage(string gifPath)
{
this.gifBitmap = new Bitmap(gifPath);
this.bitmapSource = this.GetBitmapSource();
this.Source = this.bitmapSource;
}

/// <summary>
/// 从System.Drawing.Bitmap中获得用于显示的那一帧图像的BitmapSource
/// </summary>
/// <returns></returns>
private BitmapSource GetBitmapSource()
{
IntPtr handle = IntPtr.Zero;

try
{
handle = this.gifBitmap.GetHbitmap();
this.bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, System.Windows.Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
finally
{
if (handle != IntPtr.Zero)
{
DeleteObject(handle);
}
}
return this.bitmapSource;
}

/// <summary>
/// Start animation
/// </summary>
public void StartAnimate()
{
ImageAnimator.Animate(this.gifBitmap, this.OnFrameChanged);
}

/// <summary>
/// Stop animation
/// </summary>
public void StopAnimate()
{
ImageAnimator.StopAnimate(this.gifBitmap, this.OnFrameChanged);
}

/// <summary>
/// Event handler for the frame changed
/// </summary>
private void OnFrameChanged(object sender, EventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
ImageAnimator.UpdateFrames(); // 更新到下一帧
if (this.bitmapSource != null)
{
this.bitmapSource.Freeze();
}

 Convert the bitmap to BitmapSource that can be display in WPF Visual Tree
this.bitmapSource = this.GetBitmapSource();
Source = this.bitmapSource;
this.InvalidateVisual();
}));
}

/// <summary>
/// Delete local bitmap resource
/// Reference: http://msdn.microsoft.com/en-us/library/dd183539(VS.85).aspx
/// </summary>
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DeleteObject(IntPtr hObject);
} 

然后调用方式如下:

 private GifImage gifImage;
        public MainWindow()
        {
            InitializeComponent();
            this.gifImage = new GifImage("ProgressIndicator.gif");
            this.gifImage.Width = 100;
            this.gifImage.Height = 100;
            this.Content = this.gifImage;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Popup dd = new Popup();
            this.gifImage.StartAnimate();
        }

经测试,可以在xp下完美显示

 

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要设置GIF的尺寸大小,可以使用WPF的Image控件,并设置它的Width和Height属性。同时,为了能够播放GIF动画,需要使用WPFGif动画控件,例如WpfAnimatedGif库。 以下是一个示例代码,演示如何通过WpfAnimatedGif库将GIF动画显示WPF应用程序中,并设置其尺寸大小。 首先,在XAML中,添加Image控件和Gif动画控件的引用: ```xml <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpg="clr-namespace:WpfAnimatedGif;assembly=WpfAnimatedGif" Title="MainWindow" Height="350" Width="525"> <Grid> <Image x:Name="gifImage"/> </Grid> </Window> ``` 然后,在代码中,加载GIF动画并将其设置为Image控件的Source属性: ```csharp using System.Windows.Media.Imaging; using WpfAnimatedGif; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //加载GIF动画 BitmapImage bitmap = new BitmapImage(); bitmap.BeginInit(); bitmap.UriSource = new Uri("yourGif.gif", UriKind.Relative); bitmap.EndInit(); //将GIF动画设置为Image控件的Source属性 ImageBehavior.SetAnimatedSource(gifImage, bitmap); //设置Image控件的宽度和高度 gifImage.Width = 200; gifImage.Height = 200; } } ``` 注意,上述代码中的"yourGif.gif"应该改为你的GIF动画文件的路径。同时,需要在代码中添加对WpfAnimatedGif库的引用。 通过上述代码,就可以将GIF动画显示WPF应用程序中,并设置其尺寸大小。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值