Set a Welcome Image Using C#

This article shows how to set a Welcome Image (an image shown on every Windows startup) using C#.

Introduction

In this article we will build a simple Windows Forms application that will set a Welcome Image for our Windows.

That is, every time we start our Windows the image will be shown.

Let me remind you of one thing that I have shown you previously, that is how to set a Welcome Message on every Windows Startup.

(For that you can visit here: Set a Welcome Greet on Every Windows Startup Using C#)

Background Idea

The concept is the same as in the previous article; that is, we will save the image file from one source to the following destination:

"C:\Users\<User-Name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

Procedures

Step 1: Create a new “Windows Forms Application” in Visual Studio and name it as you choose (I here named it "WelcomeImage").

Now a new form is created.

Form

Step 2: Now go to the toolbox and add two Button Controls to the project and resize the window.

The form will look like this:

Step 3: Add the following using the directive:
 

using System.IO;

Step 4: Now navigate to the code file (Form1.cs) and the following to your project:

public partial class Form1 : Form

{

     //Getting the Path "C:\Users\<User-

    //Name>\AppData\Roaming\Microsoft\Windows\Start

   // Menu\Programs\Startup" in destPathName

   string destPathName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup); 

   string imgFileName = "";

   string sourceImgPathName = "";            

   public Form1()

   {
   }
}

Step 5: Now generate the event for the "Set Image" Button and add the following code:

private void buttonSetImg_Click(object sender, EventArgs e)

{

      //Filtering the openfileDialog's File Filter by

      // setting it to 'png','jpg','jpeg'

     //that is it only shows the file With Extension

     //'.png','.jpg','.jpeg'

     openFileDialog1.Filter = "Jpg File (.jpg)|*.jpg|Png

     File (.png)|*.png|Jpeg File (.jpeg)|*.jpeg";

     //Initially Setting File Name to empty

     openFileDialog1.FileName = "";

     //Open the File Dialog and saving the return

     // result to result variable of DialogResult type

     DialogResult result=  openFileDialog1.ShowDialog();

     //Checking whether the User have Selected the

     // Image or not by checking the result variable

     if (result == DialogResult.OK)

     {

          //Getting the Source Path of the Image File

          sourceImgPathName = openFileDialog1.FileName;

          //Getting only the File Name of the Selected

         // Image

         imgFileName =Path.GetFileName(sourceImgPathName);          

         //Copy the Source Image File to the Destination

        // Path with the Name Welcome.jpg

         File.Copy(sourceImgPathName,

         Path.Combine(destPathName, "Welcome.jpg"), true);     

        MessageBox.Show("Welcome Image has been Set Successfully, Please Restart to See your Welcome Image"); 

     }

    else

    {

         MessageBox.Show("No any Image Is selected!!!");

     }

}

Saving the image file is done that is the code for saving the image file is all done and whenever you start your Windows you will see that image.

Now it's time to code for the button's "Remove Image" that is if you want that image to be removed you need to add these codes.

Step 6: Add the following line of code to your "Remove Image" Button Event section:

private void buttonRmvImg_Click(object sender, EventArgs e)

{

        //Settingthe Path of the Image

        string destImgPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup) + @"\Welcome.jpg";           

       //If the Image is there that is if the Selected

       // Image Path Exists

       if (File.Exists(destImgPath))

       {

            //Deleting the Image

            File.Delete(destImgPath);

            MessageBox.Show("Image has been Removed Successfully!!!");

        }

        else

        {

            MessageBox.Show("File Doesn't Exist in the Current Context First Try To save it!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
}

Step 7: Now compile it and run it.

After you select an image you will get that image to be shown whenever you start your Windows.

Also you can remove all these things by just pressing the "Remove Image" Button.

That's all for this article. Enjoy the Welcome Image (your welcome image can be anything ;)) on every startup. I am embedding the Source File so that you can go through it.