在打开文件对话框之前显示图片框中的默认图片

noo*_*mer 3 c# image openfiledialog picturebox

基本上,我有一个浏览按钮来打开文件对话框并将图片提取到文本框和图片框中。但是,我希望在用户打开文件对话框之前显示默认图片(如 Facebook 默认个人资料图片)。当用户打开文件对话框,选择一张图片并点击确定后,默认图片将变为选中的图片。如果用户单击取消,则默认图片不会更改。

我的问题:

  1. 我应该把默认图片放在哪里?(在与 .SLN 或其他任何地方相同路径的文件夹中)?
  2. 我应该编写什么代码才能在我的图片框中显示默认图片?

这是我的以下代码:

private void buttonbrowse_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "image files|*.jpg;*.png;*.gif";
    DialogResult dr = ofd.ShowDialog();

    if (dr == DialogResult.Cancel)
        return;

    pictureBoxPhoto.Image = Image.FromFile(ofd.FileName);
    textBoxPhoto.Text = ofd.FileName;

}
Run Code Online (Sandbox Code Playgroud)

Oti*_*iel 5

  1. 我建议将您的图片添加到您的项目资源中:

    Visual Studio 屏幕截图

    然后它会被存储到你的项目的 Resources 文件夹中:

    Visual Studio 屏幕截图

  2. 然后,您可以将其添加到表单构造函数中的图片框:

    public Form1() {
        InitializeComponent();
        pictureBox1.Image = Properties.Resources.DefaultPicture;
    }
    
    Run Code Online (Sandbox Code Playgroud)