Splash screen in C#
How come every time I build something small, no matter how trivial it could be, I wanted to share it? :-/
This afternoon, i was asked on how to do a splash screen in C#. The guy already tried threads to display they screen and and that threw an ugly exception at the user’s face. I guess it is not that simple to guess 😛
The splash screen is just a form. You can either show it on the foreground and allow your application to load on the background with thread, or just build another application and execute it with some function in the System.Diagnostic namespace, I will demo how to make a splash screen for the later since it’s easier to debug and you can show as many of them as once as you like 😛
First, you need a form, splash screen doesn’t have title bar and stuff, so change the FormBorderStyle property to none, insert a picture box and fill-docking it to the form. Why a picture box and not just the form’s background you ask? Well, the picture box is designed to host pictures and you can scale it, provide an error image for it etc. If you simply use the form itself, you’ll have to scale the picture by hand.
Next change the image attribute of the picture box to your splash bitmap. Alternatively, if you want to reuse the program without having to recompile the project, have it read the splash bitmap in the application’s directory with:
String strPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Substring(6); Bitmap img = new Bitmap(strPath + System.IO.Path.DirectorySeparatorChar + "splash.bmp"); pictureBox1.Image = img;
Put that in the Form_Load() event. The first line get the application’s directory; the Substring() is there to remove the file:// part form the returned path. The second line’s Bitmap constructor doesn’t like that 🙂
For the final touch, add a timer, set it to enabled, set your desired splash duration for interval, add a Tick event and type in
That’s it, F5 and you have a Splash screen to go
Download the source (dynamically load a bitmap)
Download the source (embedded bitmap in picturebox)
PS: That’s my actual Eclipse splash, I photoshopped it for personal use since I can’t stand the stock splash xD. But since I’m releasing this I guess I’ll have to add the source: I took it from here.
PPS: Argh, byet hosts are not compatible with WordPress’s integrated picture management T__T