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));
              }
      
  • 相关阅读:
    Maven工程运行环境修改
    Maven中出现org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException错误
    IDEA创建maven_web工程后,右键包没有Servlet、Filter、Listener选项
    Spring中的依赖注入
    什么是JavaBean
    mybatis配置SqlMapConfig.xm时没有提示
    JDK1.8之后匿名内部类访问方法中的局部变量不用加final修饰
    架构、框架和设计模式
    CitrixSQL Server 2016高可用之SQL镜像 SQL Server mirror 带见证服务器
    CitrixPVS BDM启动模式创建虚机 BDM模式部署桌面(精华)
  • 原文地址:https://www.cnblogs.com/zzr-stdio/p/12303541.html
Copyright © 2011-2022 走看看