private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(Application.StartupPath + "\\" + listBox1.SelectedItem.ToString());
        }
还有你调试中看一下 string path = this.openFileDialog1.FileName;这里的path与
=Image.FromFile(this.path +"\\"+listBox1.SelectedItem.ToString())中的path是不是一个变量 ,
前面已包含路径与文件名,后者应该是路径,所以后者要取出前者的路径才是。

解决方案 »

  1.   


      public string filepath;
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "*.gif|*.jpg";
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    string  path = this.openFileDialog1.FileName;
                     filepath=Path.GetDirectoryName(path); 
                    FileInfo f = new FileInfo(path);
                   //// AddFile(f.FullName);
                   // string strTmp = Convert.ToString(num);
                   // for (int i = 1; i <= 5 - strTmp.Length; i++)
                   //     strTmp += ' ';
                   // strTmp += "--" + f.Name;
                    this.listBox1.Items.Add(f.Name);
                    //   pictureBox1.Image = listBox1.DataSource;
                }
            }        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                pictureBox1.Image = Image.FromFile(filepath + "\\" + listBox1.SelectedItem.ToString());
            }
      

  2.   

    string path = this.openFileDialog1.FileName;
    这里把Path定义在外面
    private path;然后这行写成 Path.GetDirectoryName( this.openFileDialog1.FileName);