2014-01-23 42 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

相关问题