C# 保持图像的形式

C# 保持图像的形式,c#,C#,我做了两张表格。Form2具有设置背景图像的按钮。我明白了: this.BackgroundImage = new Bitmap(Properties.Resources._1334821694552, new Size(800, 500)); //_1334821694552 is name of the image 如何通过点击按钮更改所有表单中的背景,直到选择另一张图片为止?如果我理解正确,您有一个表单form2,其中包含更改表单form1上背景图像的按钮 我想知道你为什么在form2中

我做了两张表格。Form2具有设置背景图像的按钮。我明白了:

this.BackgroundImage = new Bitmap(Properties.Resources._1334821694552, new Size(800, 500));
//_1334821694552 is name of the image

如何通过点击按钮更改所有表单中的背景,直到选择另一张图片为止?

如果我理解正确,您有一个表单
form2
,其中包含更改表单
form1
上背景图像的按钮

我想知道你为什么在
form2
中使用
this.BackgroundImage
来更改背景图像?这将更改
form2
上的背景图像,而不是
form1
上的背景图像

实际上,您应该将对
form1
的引用传递给
form2
,然后使用
form1.BackgroundImage=…

这一切都有点模糊-我建议你编辑你的问题,实际添加一些关于
form1
form2
之间关系的信息


首先,您需要将
form1
的实例传递给
form2
。这可以在构造函数中完成,例如,假设在
form1
中打开
form2
。例如:

Settings.Default["formBackgroundImage"] = "_1334821694552";
格式2中的代码

// In form2, there's a member variable that holds the form1 reference
private form1 m_form1Instance;

// The constructor of form2 could look like this
public form2(form1 form1Instance)
{
    ...

    // Remember the form1-instance
    m_form1Instance = form1Instance;
}

// When a button is clicked in form2, the background image in form1 is changed
private void button1_Click(object sender, EventArgs e)
{
    m_form1Instance.BackgroundImage = ...;
}
// When form2 is opened, pass "this" to the constructor. The following
// sample opens form2 as a modal dialog
using (form2 f2 = new form2(this))
{
    f2.ShowDialog(this);
}
格式1中的代码

// In form2, there's a member variable that holds the form1 reference
private form1 m_form1Instance;

// The constructor of form2 could look like this
public form2(form1 form1Instance)
{
    ...

    // Remember the form1-instance
    m_form1Instance = form1Instance;
}

// When a button is clicked in form2, the background image in form1 is changed
private void button1_Click(object sender, EventArgs e)
{
    m_form1Instance.BackgroundImage = ...;
}
// When form2 is opened, pass "this" to the constructor. The following
// sample opens form2 as a modal dialog
using (form2 f2 = new form2(this))
{
    f2.ShowDialog(this);
}
这将改变
form1
上的背景图像,而不是
form2
上的背景图像

如果要保存在会话中显示的背景图像(即,当程序重新启动时,希望显示相同的背景图像),请使用Wink的答案。这将保存用于设置的背景图像


好的,看来您希望全局更改应用程序中所有窗体上的背景图像。你必须采取另一种方式,而不是我上面描述的方式

我要做的是创建一个单例类,该类描述当前背景图像,并具有在图像更改时通知订阅者的事件。比如说:

public class BackgroundImageHolder
{
    private static BackgroundImageHolder m_instance;
    private Bitmap m_backgroundImage;

    public event EventHandler BackgroundImageChanged;

    private BackgroundImageHolder() { }

    public static BackgroundImageHolder Instance
    {
        get
        {
            if (m_instance == null) m_instance = new BackgroundImageHolder();
            return m_instance;
        }
    }

    public Bitmap BackgroundImage
    {
        get { return m_backgroundImage; }
        set {
           m_backgroundImage = value;
           OnBackgroundImageChanged();
        }
    }

    protected void OnBackgroundImageChanged()
    {
        if (BackgroundImageChanged != null)
            BackgroundImageChanged(this, EventArgs.Empty);
    }
}
无论何时打开表单(即在构造函数中),都可以查询当前图像的
BackgroundImageHolder
,并将其设置为表单:

this.BackgroundImage = BackgroundImageHolder.Instance.BackgroundImage;
然后订阅通知,以便表单可以在必要时更改背景图像:

BackgroundImageHolder.Instance.BackgroundImageChanged += BackgroundImageHolder_Changed;
您需要实现事件处理程序:

private void BackgroundImageHolder_Changed(object sender, EventArgs e)
{
    this.BackgroundImage = BackgroundImageHolder.Instance.BackgroundImage;
}
然后,要全局更改图像,只需告诉
BackgroundImageHolder
有关新图像的信息:

BackgroundImageHolder.Instance.BackgroundImage = ...;

完成了

您可以将资源的名称保存在中,然后无论何时打开表单,都可以从中加载图像。(请注意,您必须有一个名为“formBackgroundImage”的应用程序设置才能进行此操作)

大概是这样的:

Settings.Default["formBackgroundImage"] = "_1334821694552";
并在
Form1\u加载每个表单的
,该表单应具有与您编写的图像相同的图像:

this.BackgroundImage = new Bitmap( Properties.Resources.ResourceManager.GetObject(Settings.Default["formBackgroundImage"]), new Size(800, 500));
如果要更改已打开表单上的图像,可以:

a) 当窗体的事件激发时,检查背景图像是否仍然相同,如果不相同,则进行更改

b) 更改背景图像后,立即循环应用程序的打开表单,并将其设置在那里:

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));
        }

} 

所有这些都取决于您计划拥有多少个打开的表单,以及您在多少位置为用户提供了更改表单图像的能力。如果您正在处理许多表单,那么其他人在此页面上发布的解决方案可能适合您。

如果您希望所有表单都具有相同的背景,那么您应该使用。创建一个维护背景信息的主窗体,然后从该主窗体派生所有其他窗体。这样,您就不必在每个派生表单中重复逻辑,所有表单都将共享相同的行为

设置起来非常简单:

public class MasterBackgroundForm : Form
{
   public override Image BackgroundImage
   {
      get
      {
         return m_backgroundImage;
      }
      set
      {
         if (m_backgroundImage != value)
         {
            m_backgroundImage = value;
            this.OnBackgroundImageChanged(EventArgs.Empty);
         }
      }
   }

   protected override void OnLoad(EventArgs e)
   {
      base.OnLoad(e);

      // Set default background image.
      this.BackgroundImage = new Bitmap(Properties.Resources._1334821694552,
                                        new Size(800, 500));
   }

   private static Image m_backgroundImage;
}

是的,没错。Form2有按钮,问题是当我在Form2中更改背景时,它在Form1中不会更改。如何做到这一点,当我点击按钮,其中有图像,图像将保留,直到被另一个改变?不错,但有一种方法,以保持图像。就像在form2中一样,背景图像被更改,并且在其他表单中保持相同的背景,因为当我在form2中更改背景图像时,当我恢复到form1时,背景图像为空,我想保留我在form2中使用的背景图像。这很好。你能顺便记下怎么做吗。我对编程和c仍然很陌生。很抱歉,这是VB.NET,我将发布c。请查看我的最新答案。我用位图作为资源进行了尝试,它应该可以工作。是否可以通过单击按钮对所有表单执行此操作?我在新位图(Properties.Resources.ResourceManager.GetObject(Settings.Default[“formBackgroundImage”])、新大小(800500)中得到一个错误;谢谢但是,如果你不介意的话,如何在多个背景下制作。像用按钮点击来改变背景,直到使用不同的图像,背景才会消失?