Several Methods for displaying GIF images in WPF and wpfgif Images

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.