WPF 如何显示gif
来源:互联网 发布:沉迷网络的影响哲学 编辑:程序博客网 时间:2024/05/22 17:08
最近碰到了要显示表情的需求,而表情刚好是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系统下就失效了,所以果断放弃
来自周银辉的博客,原文地址:http://www.cnblogs.com/zhouyinhui/archive/2007/12/23/1011555.html
大致的思路是:使用GifBitmapDecoder类,其可以将动态GIF分解成很多帧并保存在一个列表中,每一帧为一个BitmapFrame类型的对象,其父类为BitmapSource,可以将每一帧赋值给一个Image控件的Source属性,这样可以得到针对GIF各帧的Image系列。
第四种:就是目前我在用的方法,重写下Wpf的Image控件
来自CH似水年华的博客,原文地址: http://hi.baidu.com/mych/blog/item/1eb14f545f12a752564e00be.html
首先新建一个类继承自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下完美显示
- WPF 如何显示gif
- WPF显示GIF图像
- WPF中显示GIF图片
- WPF使用MediaElement显示gif图片
- [WPF疑难]在WPF中显示动态GIF
- [WPF疑难]在WPF中显示动态GIF
- IOS中如何显示Gif
- 解决XP上WPF显示gif卡的问题
- 如何利用 WebBrowser 控件,显示 .GIF 动画?
- WINCE 如何显示PNG和GIF图片
- WINCE 如何显示PNG和GIF图片
- WINCE 如何显示PNG和GIF图片
- 如何在Delphi中显示GIF动画
- WINCE 如何显示PNG和GIF图片
- IOS中如何解析并显示Gif
- android中如何显示Gif图片
- IOS中如何解析并显示Gif
- Android中如何显示gif图片
- Linux进程的管理
- 每日一个js实例6--通过面向对象实现进度条
- 安装centos7.0 docker引擎时,解决lvm包冲突问题
- Map遍历的四种方式
- 电脑维修记录02-VMware 无法查看连接所有外围设备
- WPF 如何显示gif
- Java 日期类及其处理
- 最近博客被盗,发了很多不该发的东西,请大家见谅
- Markdown编辑器的自带例子——欢迎使用Markdown编辑器写博客
- Xcode打包生成ipa四个选项的具体区别
- Java基础复习---异常练习(一):实现老师用电脑上课
- 如何复制和粘贴命令行里的文本
- Linux(Ubuntu14.04)下安装Anaconda和Spyder
- GIT(分布式版本控制系统)