如何制作改变表单背景图像的按钮 C#

How to make button that change form background Image C#

我想制作一些应用程序,我想要一个按钮,当我点击它时,我可以浏览我计算机上文件中的图像,然后将该图像更改为表单背景

我已经尝试了很多代码,但仍然找不到一个

private void ChangeBackgroundImage_Click(object sender, EventArgs e)
        {
            //how can I open the browse dialog and choose image file
            //and after that, change that image to form backgroundimage?
        }

*我是新手

首先,创建一个按钮。您将必须使用 OpenFileDialog 来获取文件路径,之后您可以通过 lambda

配置按钮点击
this.BackgroundImageChanged += new EventHandler((object sender1, EventArgs e1) =>
        {
            //if the background image has changed
        });
        OpenFileDialog dialog = new OpenFileDialog();
        dialog.Filter = "images|*.png;*.jpeg;*.jpg";
        // You can Use "|All Files|*.*" and other to filter the files.
        if (dialog.ShowDialog() == DialogResult.OK) //if path picked
        {
            button1.Click += new EventHandler((object sender1, EventArgs e1) =>
            {
                string PathFile = dialog.FileName;
                this.BackgroundImage = Image.FromFile(PathFile);
            });
        }
        else
        {
            //if path not picked
        }