问题描述
我正在创建一个非常详细的Windows窗体应用程序,并且我知道如何通过PictureBox添加图片没有问题,但是我正在尝试在正在运行的应用程序上执行操作,以使用户能够上载他们想要的任何图像。通过此IDE甚至可以做到这一点。
解决方法
您可能会看到OpenFileDialog
框过滤后的扩展名,例如:
- .jpg,
- .gif,
- .png,
- .webp
,他们将可以从任何位置上传照片。
,我建议使用OpenFileDialog
。
使用该用户,用户可以浏览其本地文件。
因此,现在他可以选择想要的图片并打开。 OpenFileDialog
将返回一个String
和文件的位置。
现在,您可以使用文件路径来更改Background
,Button
或任何您想更改的内容。
您应该过滤允许用户通过OpenFileDialog
打开的允许文件,这样他就不会突然打开.exe
或其他路径。
也许有更好的选择,但我现在不知道。
如果还有其他问题,请提供代码示例。
,private void buttonBrowse_Click(object sender,EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp"; // custom filter
if (dlg.ShowDialog() == DialogResult.OK)
{
PictureBox1.Image = new Bitmap(dlg.FileName);
}
}
}
请查看以下问题:Load a bitmap image into Windows Forms using open file dialog