Image source setting programmatically in Metro application, image does not appear

I have a main page and a camera page in my application. On the main page there is an image that does not have the original set and button. If you press the button, it will lead you to the camera page. On the camera page, I take the image and save it in the image library on the tablet, and then go back to the main page, where I would like to set the image source for the image that I just captured and saved in my image library, Here is my corresponding code.

MainPage.xaml

<Image x:Name="imgResume" HorizontalAlignment="Left" Height="303" Margin="975,60,0,0" Grid.Row="1" VerticalAlignment="Top" Width="360" Stretch="UniformToFill" Loaded="img_OnLoaded"/> <Button x:Name="btnCamera" Content="Camera" HorizontalAlignment="Left" Margin="1128,372,0,0" Grid.Row="1" VerticalAlignment="Top" RenderTransformOrigin="2.05800008773804,0.184000000357628" Height="59" Width="108" Click="Camera_Click" IsEnabled="False"/> 

MainPage.xaml.cs

 private void img_OnLoaded(object sender, RoutedEventArgs e) { if (txtFirstName.Text != "" && txtLastName.Text != "") { try { imgResume.Source = ImageFromRelativePath(this, Windows.Storage.KnownFolders.PicturesLibrary.Path + ((App)Application.Current).candidate.FirstName + ((App)Application.Current).candidate.FirstName + "Resume.jpg"); imgResume.UpdateLayout(); } catch { imgResume.Source = ImageFromRelativePath(this, @"Assets/logo.png"); imgResume.UpdateLayout(); } btnCamera.IsEnabled = true; } } public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path) { var uri = new Uri(parent.BaseUri, path); BitmapImage result = new BitmapImage(); result.UriSource = uri; return result; } 

Camera.xaml.cs

 private async void Capture_Click(object sender, RoutedEventArgs e) { if (mediaCaptureMgr != null) { string firstName = ((App)Application.Current).candidate.FirstName; string lastName = ((App)Application.Current).candidate.LastName; string fileName = firstName + lastName + "Resume.jpg"; Windows.Storage.IStorageFile photo = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.ReplaceExisting); await mediaCaptureMgr.CapturePhotoToStorageFileAsync(Windows.Media.MediaProperties.ImageEncodingProperties.CreateJpeg(), photo); this.Frame.Navigate(typeof(BasicPersonalInfo)); } } 

The img_OnLoaded method in the MainPage.xaml file tries to set the image source to the image that I save in the image library from the Capture_click method in Camera.xaml.cs.

My problem is that the image never appears on the image on the first page! Please, help!

+4
source share
2 answers

It can also be useful for people trying to solve the related problem of updating the image from the local resource file.

 myImage.Source = ImageFromRelativePath(this, "relative_path_to_file_make_sure_build_set_to_content"); public static BitmapImage ImageFromRelativePath(FrameworkElement parent, string path) { var uri = new Uri(parent.BaseUri, path); BitmapImage result = new BitmapImage(); result.UriSource = uri; return result; } 

Code from here .

+4
source

I think this part is the problem:

 var uri = new Uri(parent.BaseUri, path); 

It seems that you are trying to download the image from the installation folder while your path is the full path, which, it seems to me, is not supported for opening files in WinRT and, of course, does not work like Uri even with the database. >

Instead, you should do something like:

 var file = await Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync(((App)Application.Current).candidate.FirstName + "Resume.jpg"); var stream = await file.OpenReadAsync(); var bi = new BitmapImage(); bi.SetSource(stream); return bi; 
+3
source

All Articles