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

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

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

18条回答
  •  盖世英雄少女心
    2020-11-22 12:23

    Thanks for your post Joel, it helped me solve WPF's absence of support for animated GIFs. Just adding a little code since I had a heck of a time with setting the pictureBoxLoading.Image property due to the Winforms api.

    I had to set my animated gif image's Build Action as "Content" and the Copy to output directory to "Copy if newer" or "always". Then in the MainWindow() I called this method. Only issue is that when I tried to dispose of the stream, it gave me a red envelope graphic instead of my image. I'll have to solve that problem. This removed the pain of loading a BitmapImage and changing it into a Bitmap (which obviously killed my animation because it is no longer a gif).

    private void SetupProgressIcon()
    {
       Uri uri = new Uri("pack://application:,,,/WPFTest;component/Images/animated_progress_apple.gif");
       if (uri != null)
       {
          Stream stream = Application.GetContentStream(uri).Stream;   
          imgProgressBox.Image = new System.Drawing.Bitmap(stream);
       }
    }
    

提交回复
热议问题