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

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

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

18条回答
  •  遇见更好的自我
    2020-11-22 12:24

    I, too, did a search and found several different solution in just a thread on the old MSDN forums. (link no longer worked so I removed it)

    The simplest to execute seems to be to use a WinForms PictureBox control, and went like this (changed a few things from the thread, most of it the same).

    Add a reference to System.Windows.Forms, WindowsFormsIntegration, and System.Drawing to your project first.

    
        
            
                
                
            
        
    
    

    Then in the Window_Loaded handler, you would set the pictureBoxLoading.ImageLocation property to the image file path that you want to show.

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        pictureBoxLoading.ImageLocation = "../Images/mygif.gif";
    }
    

    The MediaElement control was mentioned in that thread, but it is also mentioned that it is a rather heavy control, so there were a number of alternatives, including at least 2 homebrewed controls based on the Image control, so this is the simplest.

提交回复
热议问题