zoukankan      html  css  js  c++  java
  • WPF中显示GIF图片

    WPF中显示GIF图片

    1. 第一种是在WPF中内嵌WindowForm的PictureBox控件

      需要引用System.Drawing、System.Windows.Forms和WindowsFormsIntegration。

      <Window x:Class="WpfApp4.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:local="clr-namespace:WpfApp4"
              mc:Ignorable="d"
              Loaded="Window_Loaded"
              Title="MainWindow" Height="450" Width="800">
          <Grid>
              <WindowsFormsHost x:Name="host">
                  
              </WindowsFormsHost>
          </Grid>
      </Window>
      

      后台代码

      private void Window_Loaded(object sender, RoutedEventArgs e)
              {
                  System.Windows.Forms.PictureBox pictureBox = new System.Windows.Forms.PictureBox();
                  this.host.Child = pictureBox;
                  pictureBox.Image = new System.Drawing.Bitmap(@"C:UsersAdministratorPictures	img6.gif");//文件路径
              }
      
    2. 第二种是使用WpfAnimatedGif第三方组件来显示

      通过Nuget安装WpfAnimatedGif。

      示例代码如下:

      <Window x:Class="WpfApp4.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:local="clr-namespace:WpfApp4"
              xmlns:gif="http://wpfanimatedgif.codeplex.com"
              mc:Ignorable="d"
              Loaded="Window_Loaded"
              Title="MainWindow" Height="450" Width="800">
          <Grid>
              <Image gif:ImageBehavior.AnimatedSource="timg7.gif"
                     gif:ImageBehavior.AutoStart="True" gif:ImageBehavior.RepeatBehavior="Forever"/>
          </Grid>
      </Window>
      
    3. 第三种是使用MediaElement来播放,有一个局限就是图片路径必须是绝对路径,并且需要一点特殊处理来让gif能循环播放。

      示例代码如下:

      <Window x:Class="WpfApp4.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:local="clr-namespace:WpfApp4"
              xmlns:gif="http://wpfanimatedgif.codeplex.com"
              mc:Ignorable="d"
              Loaded="Window_Loaded"
              Title="MainWindow" Height="450" Width="800">
          <Grid>
              <MediaElement MediaEnded="MediaElement_MediaEnded" Source="C:UsersAdministratorPictures	img6.gif"/>
          </Grid>
      </Window>
      
      

      循环播放处理代码如下

      private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
              {
                  MediaElement mediaElement = sender as MediaElement;
                  mediaElement.Position = mediaElement.Position.Add(TimeSpan.FromMilliseconds(1));
              }
      
  • 相关阅读:
    sql 语句系列(加减乘除与平均)[八百章之第十四章]
    并发系列64章(并发概要)第一章
    redis 一百二十篇(简单介绍)之第一篇
    sql 语句系列(分割ip)[八百章之第十四章]
    sql 语句系列(字符串之裂开)[八百章之第十三章]
    sql 语句系列(字符串之父与子之间)[八百章之第十二章]
    sql 语句系列(字符串的遍历嵌入删除与统计)[八百章之第十一章]
    sql 语句系列(用魔法打败魔法)[八百章之第十章]
    sql 语句系列(列举非索引外键)[八百章之第九章]
    sql 语句系列(列举系列)[八百章之第八章]
  • 原文地址:https://www.cnblogs.com/zzr-stdio/p/12303541.html
Copyright © 2011-2022 走看看