如何制作更改窗体背景图像的按钮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
}

最新更新