how to change the image in the resource folder

2019-09-16 02:59发布

问题:

I am a student studying c# and working on winform.

I have a winform which have a splashForm which loads its background image from the resources folder. and after the splash screen in the mainForm there is a option to change the splash screen background using openFileDialog.

I want to replace the image(splashimage.jpg) from resource folder image base on the selected. And I want to copy the image from user selected to the resource folder and remove the previous image and rename the image of the newly copied image to the (splashimage.jpg).

I have this code but it does not work for the replacing the image from the resource folder base on the selected image using openFileDialog.

    var FD = new System.Windows.Forms.OpenFileDialog();
                FD.Filter = "jpeg files|*.jpg";
                if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                   System.IO.File.Copy(FD.FileName,Application.StartupPath
+ "\\" + splashimage.jpg", true);           
                }

回答1:

this is correct:

this.BackgroundImage = Image.FromFile(Application.StartupPath
+ "\\" + splashimage.jpg")

this is wrong:

this.BackgroundImage = Image.FromFile("Application.StartupPath"
+ "\\" + splashimage.jpg")