日期:2014-05-17  浏览次数:21352 次

WPF(C#)点击button选择照片,怎样让Image显示我选择的照片啊
已经获得图片路径
private void OpenFile_Click(object sender, RoutedEventArgs e)
  {
  OpenFileDialog openFileDialog = new OpenFileDialog();
  openFileDialog.Title = "选择文件";
  openFileDialog.Filter = "jpg|*.jpg|jpeg|*.jpeg";
  openFileDialog.FileName = string.Empty;
  openFileDialog.FilterIndex = 1;
  openFileDialog.RestoreDirectory = true;
  openFileDialog.DefaultExt = "jpg";
  DialogResult result = openFileDialog.ShowDialog();
  if (result == System.Windows.Forms.DialogResult.Cancel)
  {
  return;
  }
  string fileName = openFileDialog.FileName;
  this.textBox1.Text = fileName;
 
  }
但是就是不能显示出来

求帮忙啊

------解决方案--------------------
刚才实测了一下,可以啊
C# code

private void Button_Click(object sender, RoutedEventArgs e)
        {
            //最好使用WPF原生OpenFileDialog 
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Title = "选择文件";
            openFileDialog.Filter = "jpg|*.jpg|jpeg|*.jpeg";
            openFileDialog.FileName = string.Empty;
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.DefaultExt = "jpg";
            bool? result = openFileDialog.ShowDialog();
            if (result !=true)
            {
                return;
            }
            string fileName = openFileDialog.FileName;
            this.textBox1.Text = fileName;
            BitmapImage image = new BitmapImage(new Uri(fileName));
            image1.Source = image;            
        }