2013-06-03 129 views
0

嗨。這是C#Visual Studio 2010。我使用以下方法上傳圖像路徑。路徑已成功存儲在數據庫中並顯示上傳的圖像。然而,儘管我導航了其他記錄,但上傳的圖片仍然顯示。其他圖像不被檢索,只有上傳的圖像保持不變。保存後,我嘗試重新填充數據集,但出現錯誤:「URI爲空」。除路徑(photo_text.Text)中的圖像外,其他文本字段會成功刷新。使用從數據庫保存的路徑刷新圖像

private void uploadPhoto_Click(object sender, RoutedEventArgs e) 
{ 
    Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); 
    ofd.FileName = ".jpg"; 
    ofd.InitialDirectory = "C:\\Users\\Public\\Pictures"; 
    ofd.Title = "Select passport photo to upload"; 
    ofd.Filter = "Image Files (*.JPG)|*.jpg|All files (*.*)|*.*"; 

    if (ofd.ShowDialog() == true) 
    { 
     pixx.Source = new BitmapImage(new Uri(ofd.FileName)); 
     photo_text.Text = ofd.FileName; 
    } 
} 

第二種方法:

if (studentsDataSetstudentTableAdapter.Update(studentsDataSet.student) > 0) 
{ 
    MessageBox.Show("Your data has been saved!", "DATA STATUS", MessageBoxButton.OK, MessageBoxImage.Information); 
    studentsDataSetstudentTableAdapter.Fill(studentsDataSet.student); 
    pixx.Source = new BitmapImage(new Uri(photo_text.Text)); 
} 

回答

0

在存在源結合以從那裏有選擇的數據的數據的圖像的圖像特性時,「模式」應被設置爲「 TwoWay「和」UpdateSourceTrigger「改爲」PropertyChanged「 每次翻閱記錄時圖像都會發生變化!

0

我有同樣的問題。當我加載圖像出現並寫入很好。但是當我站在另一個註冊表中時,每個人的圖像都是一樣的。 在XAML中,我指出了Robert Omete,但是當我將Mode = TwoWay或OneWayToSource放置在圖像上時,沒有任何圖像顯示爲空白。

加載圖像編碼:

private void BtnCargarFoto_Click(object sender, RoutedEventArgs e) 
    { 
     OpenFileDialog OD = new OpenFileDialog(); 
     OD.Filter = "jpg(*.jpg)|*.jpg|png(*.png)|*.png|gif(*.gif)|*.gif|bmp(*.bmp)|*.bmp|All Files(*.*)|*.*"; 
     if (OD.ShowDialog() == true) 
     { 
      using (Stream stream = OD.OpenFile()) 
      { 
       //bitCoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, 
       // BitmapCacheOption.OnLoad); 
       bitCoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad); 
       ImgFoto.Source = bitCoder.Frames[0]; 
      } 
      System.IO.FileStream fs = new System.IO.FileStream(OD.FileName, System.IO.FileMode.Open); 
      foto = new byte[Convert.ToInt32(fs.Length.ToString())]; 
      fs.Read(foto, 0, foto.Length); 
     } 
    } 

XAML:

  <Image x:Name="ImgFoto" Source="{Binding Foto, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" Stretch="Fill" HorizontalAlignment="Left" 
        Height="127" Margin="38,29,0,0" VerticalAlignment="Top" Width="176"> 
      </Image> 
相關問題