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:\Users\Administrator\Pictures\timg6.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:\Users\Administrator\Pictures\timg6.gif"/>
        </Grid>
    </Window>
    
    

    循環播放處理代碼如下

    private void MediaElement_MediaEnded(object sender, RoutedEventArgs e)
            {
                MediaElement mediaElement = sender as MediaElement;
                mediaElement.Position = mediaElement.Position.Add(TimeSpan.FromMilliseconds(1));
            }
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM