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系统下就失效了,所以果断放弃
第三种:
来自周银辉的博客,原文地址: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 图片显示中的保留字符问题
在WPF中显示一张图片,本是一件再简单不过的事情.一张图片,一行XAML代码即可. 但是前段时间遇到了一件奇怪的事: 开发机上运行正常的程序,在某些客户机器上却显示不了图片,而且除了这个问题,其它运行 ...
- 在WPF中显示动态GIF
在我们寻求帮助的时候,最不愿意听到的答复是:很抱歉,在当前版本的产品中还没有实现该功能... 在WPF中显示动态的GIF图像时便遇到了这样的问题,WPF中强大的Image控件却不支持动态的GIF(其只 ...
- WPF 循环显示列表
原文:WPF 循环显示列表 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SANYUNI/article/details/79423707 项目需要 ...
- WPF 窗体显示最前端
原文:WPF 窗体显示最前端 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/jjx0224/article/details/8782845 如何做一 ...
- WPF 远程显示原图 当前主页面 工具栏 一个Window页面的元素适用一个效果
http://www.jb51.net/article/98384.htm 1.wpf远程显示原图: Stretch="Fill" + ; 主要是因为那个950和650,据显示位置 ...
- WPF 托盘显示
本文告诉大家如何在 WPF 实现在托盘显示,同时托盘可以右击打开菜单,双击执行指定的代码 NotifyIcon WPF 通过 Nuget 安装 Hardcodet.NotifyIcon.Wpf 可以快 ...
- WPF:行列显示
新建显示病人信息控件PatientElement Add-->NewItem-->WPF-->UserControl(WPF),名称:PatientElement.xmal < ...
- 在WPF中显示GIF图片并实现循环播放
WPF中有一个MediaElement媒体控件,可以来播放媒体,同时也可以显示GIF图片.但看到网上有些人说用MediaElement不能加载作为资源或内嵌的资源的GIF图片,我猜他们一定是在前台用X ...
- Winform WPF 窗体显示位置
WinForm 窗体显示位置 窗体显示的位置首先由窗体的StartPosition决定,FormStartPosition这个枚举值由如下几种情况 // 摘要: // 窗体的位置由 System.Wi ...
随机推荐
- FPFH+ICP点云配准
A, UniformSampling降噪 B, ISS计算关键点, FPFH特征 在FeatureCloud::setInputCloud中读入点云,并调用processInput进行处理: proc ...
- NIO的学习总结
1.简单画的NIO流程图 2.代码实现编程: Client: package nio; import java.io.IOException; import java.net.InetSocketAd ...
- ASCII对照
标准表 Bin (二进制) Oct (八进制) Dec (十进制) Hex (十六进制) 缩写/字符 解释 0000 0000 00 0 0x00 NUL(null) 空字符 0000 0001 01 ...
- Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯(bfs)
P2845 [USACO15DEC]Switching on the Lights 开关灯 题意 题目背景 来源:usaco-2015-dec \(Farm\ John\)最近新建了一批巨大的牛棚.这 ...
- Windows API 第16篇 GetLogicalDrivers 获取驱动器位掩码
函数原型:DWORD GetLogicalDrives(VOID);The GetLogicalDrives function retrieves a bitmask representing the ...
- Android Studio && NDK开发
Android Studio下载安装网址:http://www.android-studio.org/index.php/download/hisversion 在下载界面可以查看安装包内是否包含SD ...
- 组件:slot插槽
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...
- 学习mysql数据库
1.数据库是怎么回事 数据库是存储数据的永久空间,通过一种特殊的方式存储到硬盘中 数据库分为关系型数据库和非关系型数据库 mysql查看数据的方式为:结构化查询语言(SQL语句) 访问mysql数据库 ...
- PAT甲级——A1021 Deepest Root
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 多对多关联懒加载导致failed to lazily initialize a collection of role: 实体类, could not initialize proxy - no Session 追加配置fetch = FetchType.EAGER解决
一篇文章需要关联很多个标签,所以他们呈一对多(多对多)的关系 org.springframework.web.util.NestedServletException: Request processi ...