当我重新运行应用程序时,图片框不显示上次上传的图像

时间:2011-08-20 12:05:50

标签: c# .net winforms image picturebox

嗨我有图片框,当我点击按钮时显示图像...

但当我关闭应用程序并再次运行应用程序时,它不显示上次上传的图像,这是我的代码....

 public partial class Form1 : Form
 {
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.FileName = @"D:\";

        openFileDialog1.Filter = "png files (*.png)|*.png|jpg files (*.jpg)|*.jpg";
        openFileDialog1.CheckFileExists = true;
        openFileDialog1.CheckPathExists = true;

        if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
        {


            Image image1 = Image.FromFile(openFileDialog1.FileName);
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = image1;
            pictureBox1.BackgroundImage = image1;



        }

    }
}

当我再次重新运行应用程序时,我希望显示最后上传的图片框

1 个答案:

答案 0 :(得分:5)

然后您需要保存位置并在应用程序启动时再次加载它。

Solution Explorer,打开Properties窗口,然后打开Settings标签。创建一个名为LastImage的字符串。

从代码中,您可以保存它:

Properties.Settings.Default.LastImage = openFileDialog1.FileName;
Properties.Settings.Default.Save();

并在打开时阅读:

string myImage = Properties.Settings.Default.LastImage;
if (File.Exists(myImage))
{
  pictureBox1.Image = Image.FromFile(myImage);
  //etc...
}