Creating a Splash Screen in C# by ra00l

Introduction:

Today you will learn create a simple splash screen for your application. In order to follow this tutorial, you will need to have Visual Studio .NET and .NET Framework installed on your computer.

Getting to work:

Let’s start by creating a new C# Windows Application ( File -> New -> Project -> Visual C# Projects -> Windows Application ). Name the project SplashScreen.

Let’s leave the current form the way it is and add to our project another form. To do this, go to Project menu and click Add Windows Form. Name the class Splash.cs and click OK.

Visual Studio .NET has created another form for you. Let’s work a little on our new form. Let’s set it’s FormBorderStyle to None its StartPosition to CenterScreen. In the attached archive, I’ve created a simple image witch we will set as the BackgoundImage for our current form. Resize the form until it displays the full image. Now set the TransparencyKey property of our form to the background color of our image. As you can see, the color is Black. Also, it would be nice that this form would not be displayed in the taskbar. So set the ShowInTaskbar property to false.

Now go back to Form1 class, created by default by Visual Studio .NET. We must add some code in this class to show the Splash form before initializing Form1.

Let’s go to Form1() method. That method is the constructor of the class and it executes before any other method. Let’s add here the following code, right bellow the call to InitializeComponent method:

Thread th = new Thread(new ThreadStart(DoSplash));
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);

Let’s see what is done here. Fist, we declare a thread and tell him that when he starts, to execute the DoSplash method. The next line starts the thread. Now the started thread executes in parallel with the main application thread. To prevent the main form from appearing before the thread exits, we put the main thread to sleep for 3 seconds( 3000 milliseconds ).To be sure that the second thread will exit, we’ll leave him an extra 1 second to cleanup. . Let’s take a look at the DoSplash method that our second thread executes:

Splash sp = new Splash();
sp.ShowDialog();

Let’s see what is happening here. We declared an instance of our Splash class and displayed it. When we’ll call the abort method of the thread, all the variable declared or instantiated in that thread will be disposed.

Run the application now and see what happens. On my computer, the image is displayed correctly only if I set the color depth of my computer to 16 bits. This is a bug from Microsoft. After a few searches on google, you will find there is a fix for that. So let’s fix it.

Go to the Splash class constructor, and add the following code, after the InitializeComponent method:

Bitmap b = new Bitmap(this.BackgroundImage);
b.MakeTransparent(b.GetPixel(1,1));
this.BackgroundImage = b;

What our code does is get the image we set as BackgroundImage and creates a Bitmap object as a copy of the same image. Next, we call MakeTransparent method of the Bitmap class that gets a color as an argument and makes it transparent. The argument that we pass is the color of the pixel at coordinates 1,1(these coordinates are not the only ones that you can use. You can use as well 0,0 and any other.).

Here is the splash on my machine.

 

That’s about it. Until next time, I wish you luck and happy programming.

Raul POPESCU

Attachments:

Project Files: creating_splash_csharp.zip