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

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

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

18条回答
  •  囚心锁ツ
    2020-11-22 12:23

    Adding on to the main response that recommends the usage of WpfAnimatedGif, you must add the following lines in the end if you are swapping an image with a Gif to ensure the animation actually executes:

    ImageBehavior.SetRepeatBehavior(img, new RepeatBehavior(0));
    ImageBehavior.SetRepeatBehavior(img, RepeatBehavior.Forever);
    

    So your code will look like:

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

提交回复
热议问题