Keeping the image in form

前端 未结 3 956
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 09:43

I made 2 forms. Form2 has button which set background image. I got this :

this.BackgroundImage = new Bitmap(Properties.Resources._1334821694552, new Size(800, 50         


        
3条回答
  •  后悔当初
    2021-01-29 10:19

    You could save the name of the resource in an application settings and then whenever a form opens you could load the image from there. (Please note that you have to have an application settings with the name "formBackgroundImage" already in place to make this work).

    Something like this:

    Settings.Default["formBackgroundImage"] = "_1334821694552";
    

    and in Form1_Load of every form that should have the same image you write:

    this.BackgroundImage = new Bitmap( Properties.Resources.ResourceManager.GetObject(Settings.Default["formBackgroundImage"]), new Size(800, 500));
    

    If you'd like to change the image on forms that are already open you could:

    a) when the form's Activated event fires, check if the backgroundImage is still the same and change it if it isn't.

    OR

    b) Right after you change the backgroundImage, loop through the application's open forms and set them there as well:

    private void changeBackgroundImage()
    {
        //set the backgroundImage for this form
        this.BackgroundImage = new Bitmap(Properties.Resources._1334821694552, new Size(800, 500));
    
        //save the backgroundImage in an application settings
        Settings.Default["formBackgroundImage"] = "_1334821694552";
    
        //set the backgroundImage for all open forms in the application:
            foreach (Form f in Application.OpenForms) {
                    f.BackgroundImage = new  Bitmap(Properties.Resources.ResourceManager.GetObject(Settings.Default["formBackgroundImage"]),new Size(800, 500));
            }
    
    } 
    

    All this depends on how many open forms you plan on having and in how many locations you give the user the ability to change the image of the form. If you're dealing with many forms then the solution posted by others on this page might be right for you.

提交回复
热议问题