在 Windows 表单应用程序 C# 中添加图像
Adding images within Windows form application C#
我正在创建一个非常详细的 windows 表单应用程序,我知道如何通过图片框添加图片没问题,但是我想做的是在 运行 应用程序上能够让用户上传他们想要的任何图像。这甚至可能通过这个 IDE.
您可能会查看某些图像文件格式的 OpenFileDialog
框过滤扩展名,例如:
- .jpg,
- .gif,
- .png,
- .webp
他们将能够从任何位置上传照片。
我建议使用 OpenFileDialog
。
使用它,用户可以浏览他的本地文件。
所以现在他可以 select 想要的图片并 打开 它。 OpenFileDialog
将 return 一个带有文件位置的 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
我正在创建一个非常详细的 windows 表单应用程序,我知道如何通过图片框添加图片没问题,但是我想做的是在 运行 应用程序上能够让用户上传他们想要的任何图像。这甚至可能通过这个 IDE.
您可能会查看某些图像文件格式的 OpenFileDialog
框过滤扩展名,例如:
- .jpg,
- .gif,
- .png,
- .webp
他们将能够从任何位置上传照片。
我建议使用 OpenFileDialog
。
使用它,用户可以浏览他的本地文件。
所以现在他可以 select 想要的图片并 打开 它。 OpenFileDialog
将 return 一个带有文件位置的 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