I have a small Windows Forms project and now Iam looking to display an image at project startup, I mean Program.cs
Is it possible?
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Image MyPrgImage = Image.FromFile("C:\\Temp\\Images\\For_Network.gif");
??????
Application.Run(new Form1());
}
Sure... Add new WindowsForm to your project, call it SplashImageForm. Add PictureBox control to it, and add the image you want in it. Resize the form, set these SplashImageForm properties:
FormBorderStyle - None
ShowInTaskBar - false
StartPosition - CenterScreen
Then you want to show that form before Form1 and close it after the timeout has expired... Like so for example:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SplashImageForm f = new SplashImageForm();
f.Shown += new EventHandler((o,e)=>{
System.Threading.Thread t = new System.Threading.Thread(() =>
{
System.Threading.Thread.Sleep(2000);
f.Invoke(new Action(() => { f.Close(); }));
});
t.IsBackground = true;
t.Start();
});
Application.Run(f);
Application.Run(new Form1());
}
EDIT
Now, there is new thread which blocks on System.Threading.Thread.Sleep(2000) for 2 seconds, and the main thread is allowed to block on Application.Run(f) as it is supposed to, until the SplashImageForm isn't closed. So the image gets loaded by the main thread and the GUI is responsive.
When the timeout ends, Invoke() method is called so the main thread which is the owner of the form closes it. If this wasn't here, Cross threaded exception would be thrown.
Now the image is shown for 2 secs, and after it Form1 is shown.
You mean a splash screen, right?
Consider adding a reference to Microsoft.VisualBasic (if not already done) and then set the WindowsFormsApplicationBase.SplashScreen property.
A few more points:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With