To show animated GIF in WPF/C# I use this code sample in Microsoft MSDN : Show GIF animation in WPF.
When I use this in modeless window (window.Show()), the image do not animate. Why?
With window.ShowDialog() (modal window) it works correctly.
In WPF Project Befor Start MainWindow i show a window to do my first task in modeless and then close it.(these are in app.xaml.cs Startup event)
// app.xaml.cs
public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
FirstTask firstTask = new FirstTask();
firstTask.Show();
// do task
System.Threading.Thread.Sleep(5000);
firstTask.Close();
MainWindow mainWindow = new MainWindow();
mainWindow.ShowDialog();
}
}
I add below code end of AnimatedGIFControl_Loaded function in AnimatedGIFControl class to start animate gif automatically.
ImageAnimator.Animate(_bitmap, OnFrameChanged);
complete AnimatedGIFControl_Loaded code
void AnimatedGIFControl_Loaded(object sender, RoutedEventArgs e)
{
// Get GIF image from Resources
if (gifanimate.Properties.Resources.ProgressIndicator != null)
{
_bitmap = gifanimate.Properties.Resources.ProgressIndicator;
Width = _bitmap.Width;
Height = _bitmap.Height;
_bitmapSource = GetBitmapSource();
Source = _bitmapSource;
ImageAnimator.Animate(_bitmap, OnFrameChanged);
}
}
ImageAnimator.Animate(_bitmap, OnFrameChanged);
Also i add to firstTask window and MainWindow to show animated gif.
Another problem: after firstTask.Close(); application do not show MainWindow. did you know why?
Show() method does not block the call and continues with the execution,
ShowDialog() method blocks the call and waits with the execution until the modal dialog is closed (and also during that,all UI messages are dispatched)