How do I get an animated gif to work in WPF?

前端 未结 18 1087
广开言路
广开言路 2020-11-22 12:16

What control type should I use - Image, MediaElement, etc.?

18条回答
  •  孤城傲影
    2020-11-22 12:21

    I couldn't get the most popular answer to this question (above by Dario) to work properly. The result was weird, choppy animation with weird artifacts. Best solution I have found so far: https://github.com/XamlAnimatedGif/WpfAnimatedGif

    You can install it with NuGet

    PM> Install-Package WpfAnimatedGif

    and to use it, at a new namespace to the Window where you want to add the gif image and use it as below

    
        Title="MainWindow" Height="350" Width="525">
    
    
        
        
    

    The package is really neat, you can set some attributes like below

    
    

    and you can use it in your code as well:

    var image = new BitmapImage();
    image.BeginInit();
    image.UriSource = new Uri(fileName);
    image.EndInit();
    ImageBehavior.SetAnimatedSource(img, image);
    

    EDIT: Silverlight support

    As per josh2112's comment if you want to add animated GIF support to your Silverlight project then use github.com/XamlAnimatedGif/XamlAnimatedGif

提交回复
热议问题