码迷,mamicode.com
首页 > Windows程序 > 详细

WPF显示Gif动画

时间:2018-10-16 20:51:15      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:ati   通过   ima   ade   显示   media   https   info   程序集   

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动画

标签:ati   通过   ima   ade   显示   media   https   info   程序集   

原文地址:https://www.cnblogs.com/xinyf/p/9800459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!