High RAM Usage with Custom Splash Screen in WPF (Code-Behind, .NET Core 6) - How to Reduce Memory Consumption?

Mojtaba_Hakim 321 Reputation points
2024-10-23T02:45:31.8333333+00:00

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:

  • The splash screen causes excessive memory usage (around 1000 MB) during startup.
  • The GIF in the splash screen seems to be contributing to the problem.

What I've Tried:

  • I'm using the WPFAnimatedGif library to show the GIF, with a repeat behavior of 2x.
  • I've made the splash screen window transparent and removed the taskbar icon.

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

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,012 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,805 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,316 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,157 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
824 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 118.6K Reputation points
    2024-10-23T06:04:04.2+00:00

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.