Several Methods for displaying GIF images in WPF and wpfgif Images
Use MediaElement
One limitation of this method is that the image path must be an absolute path.
<MediaElement Source = "file: // C: \ 129.gif" />
And you also need to set it to loop
<MediaElement Source = "file: // C: \ 129.gif" MediaEnded = "MediaElement_MediaEnded" />
private void MediaElement_MediaEnded (object sender, RoutedEventArgs e)
{
((MediaElement) sender) .Position = ((MediaElement) sender) .Position.Add (TimeSpan.FromMilliseconds (1));
}
Via PictureBox control in winform
相对 This way you can specify a relative path; first, you need to add the assembly reference of the window to the wpf program: System.Drawing.dll, System.Windows.Forms.dll and WindowsFormsIntegration.dll
After referencing the type, you can use PictureBox in winform in XAML code
xmlns: wfi = "clr-namespace: System.Windows.Forms.Integration; assembly = WindowsFormsIntegration"
xmlns: winForms = "clr-namespace: System.Windows.Forms; assembly = System.Windows.Forms"
<wfi: WindowsFormsHost>
<winForms: PictureBox x: Name = "PictureOfGif"> </ winForms: PictureBox>
</ wfi: WindowsFormsHost>
绑定 Binding pictures in the program load event
void MainWindow_Loaded (object sender, RoutedEventArgs e)
{
this.PictureOfGif.Image = System.Drawing.Image.FromFile ("images / 129.gif");
}
WpfAnimatedGif
Can be installed via console or Nuget
Install-Package WpfAnimatedGif
xmlns: gif = "http://wpfanimatedgif.codeplex.com"
<Image gif: ImageBehavior.AnimatedSource = "Images / animated.gif" />
GitHub address: https://github.com/XamlAnimatedGif/WpfAnimatedGif
Related documents:
https://nnish.com/tag/animated-gif-in-wpf/
https://social.msdn.microsoft.com/Forums/vstudio/en-US/93d50a97-0d8d-4b18-992e-cd3200693337/how-to-use-an-animated-gif?forum=wpf