2013-03-25 8 views
2

내 xaml 파일에 OutputImg라는 이미지가 있습니다. 이미지의 이름을 표시하는 OutputTB라는 텍스트 블록과 내 Pictures 폴더에서 이미지를 선택할 수있는 버튼도 있습니다. 뒤에프로젝트 외부에서 이미지 추가 C# Windows Store 응용 프로그램

코드 :

private async void Button_Click_1(object sender, RoutedEventArgs e) 
{ 
    FileOpenPicker openPicker = new FileOpenPicker(); 
    openPicker.ViewMode = Picker.ViewMode.List; 
    openPicker.SuggestedStartLocation = PickerLocationId.PicutresLiibrary; 
    openPicker.FileTypeFilter.Add(".png"); 
    StorageFile.file = await openPicker.PickSingleFileAsync(); 
    OutputTB.text = file.Name; 


    BitmapImage image = new BitmapImage(new Uri(file.path)); 
    OutputImg.Source = image; 
} 

질문은 내가 오류를 얻을 해달라고하더라도, 내 사진이 표시되지 것입니다. 그것은 그림의 이름을 OutputTB.text에 기록하지만 Image는 비어 있습니다. 선택한 이미지를 OutputImg 이미지 상자에 표시하려면 어떻게합니까?

나는 거기에 내가 여기에 누락 정말 기본적인 일이 될하지만 수 있음을 이해는 단지 file.path 이전 스타일을 제공하기 때문에 당신은 비트 맵에 대한 Uri 개체를 만들 수 file.path을 사용할 수 없습니다 학습 프로젝트

답변

3

될 운명 경로 (예 : c:\users\...\foo.png). 비트 맵에 새로운 스타일의 URI 경로 (예 : ms-appdata:///local/path..to..file.../foo.png)가 필요합니다.

그러나 그림 라이브러리의 새로운 스타일 URI 경로를 지정하는 방법은 없습니다. 따라서 간단한 해결 방법 사용할 필요가 : 파일에 대한 참조를 가지고 있기 때문에

을 대신 파일의 스트림에 접근 한 다음 비트 맵의 ​​소스로 스트림을 설정할 수 있습니다

StorageFile file = await openPicker.PickSingleFileAsync(); 
OutputTB.text = file.Name; 

// Open a stream for the selected file. 
var fileStream = 
    await file.OpenAsync(Windows.Storage.FileAccessMode.Read); 

// Set the image source to the selected bitmap. 
BitmapImage image = new BitmapImage(); 
image.SetSource(fileStream); 

OutputImg.Source = image;