WPF的Image控件不能很好的支持.gif文件。解决办法有如下2种。

1使用MediaElement

<MediaElement Source="file://D:\anim.gif"/>

使用时,注意要使用  “file://”和gif文件的绝对路径。

2使用WinForm里面的PictureBox控件

Winform里面的PictureBox支持播放gif动画。我们需要的就是将winform控件停靠在wpf窗体中。这需要使用WindowsFormsHost。WindowsFormsHost存在于程序集WindowsFormsIntegration.dll的System.Windows.Forms.Integration命名空间。

第一步,添加WindowsFormsIntegration.dll和System.Windows.Forms.dll引用。

然后添加下列命名空间到XAML。

<Window x:Class="WpfApplication2.Window1"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:winForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

现在可以在wpf中放置PictureBox控件了

 <Grid>
<wfi:WindowsFormsHost>
<winForms:PictureBox x:Name="pictureBoxLoading">
</winForms:PictureBox>
</wfi:WindowsFormsHost>
</Grid>

在code behind(Window1.xaml.cs),添加到Window_Loaded Event:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.pictureBoxLoading.Image = System.Drawing.Image.FromFile("anim.gif"); ;
}

注意:

  1. GIF文件路径正确
  2. 也可以通过 resource 文件导入.

参考资料:

Animated Gif in WPF using Windows Forms Control

WPF显示Gif动画的更多相关文章

  1. WPF 显示GIF动画

    简单的通过GifBitmapDecoder解析GIF图片,获取gif帧数和每一帧数据,然后通过时间切换显示,效果如下: 代码如下: namespace GIfImageApplication { pu ...

  2. WPF编程学习——动画

    前言 使用动画,是增强用户体验的一种有效的手段.合理的动画,可以让应用程序的界面看起来更加自然.真实.流畅.舒适,更有效地向用户展现信息,用户也更容易接受.同时也增加了软件使用的乐趣,提高用户粘度.( ...

  3. WPF 有趣的动画效果

    WPF 有趣的动画效果         这一次我要呈上一个简单的文章,关于给你的WPF apps加入美丽的光线动画,可是我对动画这东西可能有点入迷了.         实际上.我对动画如此的入迷,以至 ...

  4. WPF中的动画——(三)时间线(TimeLine)

    WPF中的动画——(三)时间线(TimeLine) 时间线(TimeLine)表示时间段. 它提供的属性可以让控制该时间段的长度.开始时间.重复次数.该时间段内时间进度的快慢等等.在WPF中内置了如下 ...

  5. ActionBar compat 如何禁用ActionBar的显示/隐藏动画

    ActionBar compat 如何关闭ActionBar的显示隐藏动画 @Override public boolean onCreateOptionsMenu(Menu menu) { //消除 ...

  6. WinForm上显示gif动画:转

    WinForm上的ProgressBar,老实说,实在是不敢恭维,太死板,太难看了,即使做成实时显示处理进度的,它还是逃离不了“难看”.现 在的web2.0上到处都能看到一个显示正在处理的小圆圈在那转 ...

  7. C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试(续)

    介绍 本文是接着上文<C# WPF 显示图片和视频显示 EmuguCv.AForge.Net测试>写的,建议先看下上文,因为有些代码还需要了解. 增添 接着上文的代码,我们可以在事件处理方 ...

  8. Android窗口管理服务WindowManagerService显示窗口动画的原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8611754 在前一文中,我们分析了Activi ...

  9. Android显示GIF动画完整示例(二)

    MainActivity如下: package cc.testgif2; import android.os.Bundle; import android.app.Activity; /** * De ...

随机推荐

  1. Linux安装imagick扩展出现错误:configure: error: not found. Please provide a path to MagickWand-config or Wand-config program.

    在Linux(CentOS)上安装imagick扩展时,遇到如下错误: checking ImageMagick MagickWand API configuration program... che ...

  2. jQuery操作table tr td

    1.鼠标移动行变色 $("#tab tr").hover(function(){ $(this).children("td").addClass("h ...

  3. MPI 派生数据类型 MPI_Type_create_struct(),MPI_Type_contiguous(),MPI_Type_vector(),MPI_Type_create_hvector(),MPI_Type_indexed()

    ▶ 使用 MPI 派生数据类型,减少数据在传输过程中的耗时 ● MPI_Type_create_struct() 范例代码 { ; int globalDataInt[globalSize], glo ...

  4. leetcode232

    public class MyQueue { Stack<int> S = new Stack<int>(); /** Initialize your data structu ...

  5. leetcode28

    public class Solution { public int StrStr(string haystack, string needle) { return haystack.IndexOf( ...

  6. Using DirectShow------------------msdn

    Using DirectShow   This section contains the following topics: Simulating Graph Building with GraphE ...

  7. maven工程 ,通过maven更新后,jre恢复到1.5的解决方法

    在maven setting.xml profiles节点下加入 <profile> <id>jdk-1.8</id> <activation> < ...

  8. 【317】python 指定浏览器打开网页 / 文件

    一.python 打开浏览器的方法: 1. startfile方法(打开指定浏览器) import os os.startfile("C:\Program Files\internet ex ...

  9. 归纳整理Linux下C语言常用的库函数----内存及字符串控制及操作

    在没有IDE的时候,记住一些常用的库函数的函数名.参数.基本用法及注意事项是很有必要的. 参照Linux_C_HS.chm的目录,我大致将常用的函数分为一下几类: 1. 内存及字符串控制及操作 2. ...

  10. go_字符和字符串处理

    rune相当于go的char 使用range遍历pos,rune对 使用utf8.RuneCountInString(s)获得字符数量 使用len获得字节长度,使用[]byte获得字节 一般把字节转成 ...