2012-08-06 2 views
4

내 응용 프로그램에 기본 페이지와 카메라 페이지가 있습니다. 기본 페이지에는 원본 세트 및 버튼이없는 이미지가 있습니다. 버튼을 클릭하면 카메라 페이지로 이동합니다. 카메라 페이지에서 이미지를 캡처하여 태블릿의 그림 라이브러리에 저장 한 다음 기본 이미지 페이지로 돌아가서 방금 촬영 한 이미지를 내 그림 라이브러리에 저장하고 싶습니다 . 여기에 관련 코드가 있습니다.메트로 앱에서 프로그래밍 방식으로 이미지 소스를 설정하면 이미지가 나타나지 않습니다.

에서 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에서 MainPage.xaml 파일의 img_OnLoaded 방법을 시도

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)); 
     } 
    } 

Camera.xaml.cs의 Capture_click 메서드에서 이미지 라이브러리에 저장 한 이미지로 이미지 소스를 설정합니다.

제 문제는 그림이 첫 페이지의 이미지에 나타나지 않는다는 것입니다. 도와주세요!

+0

두 답변을 모두 시도했지만 나에게 도움이되지 않습니다. 이 페이지에서 어떤 해결책을 찾았지만 완전히 작동합니다. [http://www.c-sharpcorner.com/UploadFile/99bb20/load-image-dynamically-from-application-uri-in-windows-store/](http://www.c-sharpcorner.com/UploadFile)/99bb20/load-image-dynamic-from-application-uri-in-windows-store /) –

답변

4

:

대신 뭔가를해야한다.

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; 
} 

코드는 here입니다.

+0

이것은 올바른 길로 가고 있어요. 이미지 소스를 참조 해제 한 다음 이미지를 삭제하고 레이아웃을 업데이트하여 이미지를 이미지에서 제거해야했습니다. –

+0

이렇게 간단하고 공통적 인 일을하는 코드가 너무 많습니다. Microsoft가 myImage = new Image (string imagePath)와 같은 것을 제공 할 수없는 이유는 무엇입니까? ? 아마 그들이 가입 한 어떤 패턴이나 다른 동그라미도 고수하지 않을 것입니다. – nedR

3

나는이 부분이 문제라고 생각합니다 : 경로 내가 WinRT에서 파일을 열 지원되지 않습니다 믿습니다 전체 경로가있을 때 설치 폴더에서 이미지를로드 할 것으로 보인다

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

확실히베이스 리 (baseUri)를 가지고도 우리처럼 작동하지 않습니다. 사람들이 로컬 리소스 파일에서 이미지를 업데이트 관련 문제를 해결하기 위해 노력에 이것은 또한 도움이 될 수 있습니다

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; 
관련 문제