【问题标题】:C# WPF gif black backgroundC# WPF gif 黑色背景
【发布时间】:2015-12-31 20:22:27
【问题描述】:

我对 MediaElement 中的 Gif 动画有疑问。动画还可以,但透明背景是黑色的,而且我看到所有其他帧。我尝试了其他 Gif 和它是相同的。

有我的 MediaElement:

<MediaElement x:Name="myGif" 
              Height="100" HorizontalAlignment="Left" 
              Width="100" VerticalAlignment="Top" 
              Margin="100,100,0,0" 
              MediaEnded="myGif_MediaEnded"  
              UnloadedBehavior="Manual" 
              LoadedBehavior="Play" 
              Source="animation.gif" Stretch="Fill"/>

当我使用图像标签时 - 透明是可以的,但令人惊讶的是动画不起作用。

【问题讨论】:

    标签: c# wpf animation gif transparent


    【解决方案1】:

    您可以考虑为此使用一个小型库。 Thomas Levesque 创建了一个 WpfAnimatedGif,可作为 Nuget 包使用。只需在包管理器控制台中输入以下内容:

    Install-Package WpfAnimatedGif 
    
    PM> Install-Package WpfAnimatedGif
    Installing 'WpfAnimatedGif 1.4.13'.
    Successfully installed 'WpfAnimatedGif 1.4.13'.
    Adding 'WpfAnimatedGif 1.4.13' to AnimatedGIF.
    Successfully added 'WpfAnimatedGif 1.4.13' to AnimatedGIF.
    

    然后让我们定义一个小 GUI 来测试这个库:

    <Window x:Class="AnimatedGIF.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Hypnotize"
            xmlns:wpfanimated="http://wpfanimatedgif.codeplex.com"
            Height="350" Width="525">
    
        <Grid>    
    
            <Image wpfanimated:ImageBehavior.AnimatedSource="a_transparent_gif.gif"
                   wpfanimated:ImageBehavior.RepeatBehavior="1"
                   wpfanimated:ImageBehavior.AnimateInDesignMode="False"
                   wpfanimated:ImageBehavior.AutoStart="True"              
                   Height="300" />
    
        </Grid>
    </Window>
    

    在这里,我们使用图像控件并将库提供的功能作为附加属性应用。源代码在 Github 页面上提供:

    https://github.com/thomaslevesque/WpfAnimatedGif/blob/master/WpfAnimatedGif/ImageBehavior.cs

    我在这里提供了一个示例解决方案:

    https://onedrive.live.com/redir?resid=8EF5059044F781FC!40935&authkey=!AA7kju6NnsGzpB4&ithint=file%2czip

    现在我们都忘记了动画 GIF 曾经被认为是多么酷吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 2021-11-10
      • 2020-12-13
      • 2015-03-26
      • 1970-01-01
      相关资源
      最近更新 更多