Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Image at project startup - program.cs?

Tags:

c#

winforms

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());
        }
like image 531
Paramu Avatar asked Dec 11 '25 16:12

Paramu


2 Answers

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.

like image 168
Cipi Avatar answered Dec 14 '25 06:12

Cipi


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:

  • Windows Forms doesn't have support for a simple and straight splash screen.
    Even the solution above will take a few seconds until the .net framework is loaded to show the splash screen.
  • See this question here for further examples and important remarks.
  • See this CodeProject.com sample for a custom solution
like image 27
Marc Avatar answered Dec 14 '25 07:12

Marc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!