2014-01-23 43 views
0

我想用C#和XAML如何路徑在C#中來自圖片庫的圖像

這裏我的代碼在我的圖片庫的應用程序(的MediaElement)顯示圖片:

media.PosterSource = new BitmapImage(new Uri("file//C:/Users/Siio/Pictures/bird.jpg", UriKind.Absolute)); 

但形象贏得不會出現。

有什麼建議嗎?

更新 C#

StorageFolder photoFolder = KnownFolders.PicturesLibrary; 
List<string> photoTypeFilter = new List<string>(); 
photoTypeFilter.Add(".png"); 
QueryOptions queryOption = new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter); 
StorageFileQueryResult queryResults = photoFolder.CreateFileQueryWithOptions(queryOptions); 
IReadOnlyList<StorageFile> filess = await queryResult.GetFilesAsync(); 
foreach (StorageFile fil in filess) 
{ 
    MusicProperties musicProperty = await fil.Properties.GetMusicPropertiesAsync(); 
    if (musicProperty.Title == UpdateFav.F_Title) 
    { 
      Uri uri = new Uri(@"C:\Users\Christopher\Pictures\" + UpdateFav.F_Title + ".png"); 
      Uri uri1 = new Uri(uri.AbsoluteUri); 
      BitmapImage im = new BitmapImage(uri1); 
      media.PosterSource = im; 
    } 
} 

XAML:

<MediaElement x:Name="media" HorizontalAlignment="Left" Height="398" Margin="122,63,0,0" Grid.Row="1" VerticalAlignment="Top" Width="592" AudioCategory="BackgroundCapableMedia" PosterSource="Assets/BG2.jpg" MediaOpened="media_MediaOpened"/> 

回答

0

你絕對文件路徑語法不正確。它在「文件」後缺少冒號。正確的語法是「file:///」。這應該導致System.UriFormatException

Uri構造函數已經知道如何通過爲你附加「file:///」語法來處理文件路徑。下面是一個更簡單的指定文件路徑的方法。

實施例:

string fileName = @"c:\temp\foo.txt"; 
Uri uriFile = new Uri(fileName); 
Console.WriteLine("Absolute Path: {0}", uri.AbsolutePath); 
Console.WriteLine("Absolute Uri: {0}", uri.AbsoluteUri); 

輸出:

絕對路徑:C:/temp/foo.txt
絕對URI:文件:/// C:/temp/foo.txt

培訓相關問題:c# type to handle relative and absolute URI's and local file paths

相關問題