3

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());
        }
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Paramu
  • 613
  • 2
  • 10
  • 23

4 Answers4

6

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.

Cipi
  • 11,055
  • 9
  • 47
  • 60
  • But the problem is picturebox display no problem. But the image is not displaying with picturebox... – Paramu Sep 26 '11 at 13:21
  • 1
    Try now. Check the code again, I edited it to add new thread that will be blocked instead of the old code where the main thread was blocked. – Cipi Sep 26 '11 at 13:35
  • @Cipi: what I refer to is purely a personal preference ...for instance, rather than fixing the splash screen to 2 seconds, I would have another event, like `Form1.HandleCreated`, that closes the splash screen. Like I said, you're not wrong by any means (I +1 your answer) ...the closing is just personal preference. – IAbstract Sep 26 '11 at 21:54
  • Hey Cipi that looks like a nice example (+1), but Form1 would only be created once the splash screen is closed. So if the creation of Form1 takes a lot of time there would still be a moment where nothing is shown on screen. It would be nice to have Form1 directly shown after SplashImageForm is closed. – Marc Sep 27 '11 at 06:40
3

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
Community
  • 1
  • 1
Marc
  • 9,012
  • 13
  • 57
  • 72
  • @IAbstract can you explain why not? I agree that it seem weird, but you also add a reference to other DLLs just to use their features. VisualBasic.dll is maintained by MS and an integral part of the .net framework, so I don't see any problem with this. – Marc Sep 26 '11 at 12:08
  • See @Cipi's answer ...that doesn't use the reference. I don't necessarily agree with the process he uses for closing the form, but it isn't incorrect. – IAbstract Sep 26 '11 at 12:11
  • @IAbstract: Whats wrong with form closing in my code? It is thread safe, and it closes the form... :) – Cipi Sep 26 '11 at 21:27
  • @IAbstract: Would like to see your idea... Post it as a comment here and I will add it to my answer with a reference to you. ;) – Cipi Sep 26 '11 at 21:40
1

You would need a simple form, perhaps with a PictureBox, to loadd and display the image. Then remove it once your main form is loaded.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • But the problem is picturebox display no problem. But the image is not displaying with picturebox... – Paramu Sep 26 '11 at 13:19
0

Simply add a windows form(let the name of form be imgsplash) & from option set following:-

FormBorderStyle - None
ShowInTaskBar - false
StartPosition - CenterScreen

in this form set background image[image which you want to show at startup of application]

--now in program.cs add folloing steps:-

       static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        imgsplash f = new imgsplash();
        f.Show();
        System.Threading.Thread.Sleep(2000);
        f.Close();
        Application.Run(new Form1());
    }
ajeet
  • 1