Try the XamlAnimatedGif instead of WpfAnimatedGif:
Or maybe consider the GifBitmapDecoder class and tasks or threads to extract and display the frames of GIF file.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I'm working on a WPF application using .NET Core 6 (Code-Behind, no MVVM) in Visual Studio 2022 on Windows 10. I've created a customized splash screen that displays a GIF on startup. The splash screen closes once the app has fully loaded.
My problem is that the splash screen causes high memory usage (up to 1000 MB) as seen in the Task Manager. I want to reduce the memory consumption, but I can't figure out how to optimize this.
XAML for Splash Screen:
<Window x:Class="Prg_UI.SplashScreen.WinSplashy"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:gifplayer="http://wpfanimatedgif.codeplex.com"
Background="{x:Null}"
AllowsTransparency="True"
WindowStyle="None"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
ShowInTaskbar="False"
Loaded="Window_Loaded"
Width="501" Height="308">
<Grid>
<Rectangle RadiusX="10" RadiusY="10">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#FF09D4AA" Offset="1"/>
<GradientStop Color="#FF14D66E"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Image x:Name="myg" Margin="-9,8,4,5" Visibility="Hidden"
gifplayer:ImageBehavior.RepeatBehavior="2x"
Stretch="UniformToFill"
gifplayer:ImageBehavior.AutoStart="True"
gifplayer:ImageBehavior.AnimatedSource="/UiDrive/IMGS/SplashHandwrite.gif"/>
<Border BorderBrush="White" BorderThickness="1" CornerRadius="10"/>
</Grid>
</Window>
Code-Behind for Splash Screen:
using System.Windows;
namespace Prg_UI.SplashScreen
{
public partial class WinSplashy : Window, ISplashScreen
{
public WinSplashy()
{
InitializeComponent();
}
public void LoadComplete()
{
Dispatcher.InvokeShutdown();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
myg.Visibility = Visibility.Visible;
}
}
public interface ISplashScreen
{
void LoadComplete();
}
}
Splash Screen Initialization in App.xaml.cs:
public partial class App : Application
{
public static ISplashScreen splashScreen;
private ManualResetEvent ResetSplashCreated;
private Thread SplashThread;
protected override void OnStartup(StartupEventArgs e)
{
ResetSplashCreated = new ManualResetEvent(false);
SplashThread = new Thread(ShowSplash);
SplashThread.SetApartmentState(ApartmentState.STA);
SplashThread.IsBackground = true;
SplashThread.Name = "Splash Screen";
SplashThread.Start();
ResetSplashCreated.WaitOne();
base.OnStartup(e);
}
private void ShowSplash()
{
WinSplashy animatedSplashScreenWindow = new WinSplashy();
splashScreen = animatedSplashScreenWindow;
animatedSplashScreenWindow.Show();
ResetSplashCreated.Set();
System.Windows.Threading.Dispatcher.Run();
}
}
Closing the Splash Screen in MainWindow.xaml.cs:
public MainWindow()
{
InitializeComponent();
App.splashScreen.LoadComplete();
}
Issue:
What I've Tried:
WPFAnimatedGif
library to show the GIF, with a repeat behavior of 2x
.Question:
How can I reduce the memory usage of the splash screen without compromising the GIF functionality? Is there something I'm missing in managing the GIF or threads efficiently? Any suggestions or improvements to optimize memory usage would be appreciated.
Thanks you
Try the XamlAnimatedGif instead of WpfAnimatedGif:
Or maybe consider the GifBitmapDecoder class and tasks or threads to extract and display the frames of GIF file.