C# 从图片库设置背景图像
我必须以编程方式设置一个网格的背景,其中包含从图片库(应用程序的“无资源”文件夹)拍摄的图像 我已尝试使用此代码C# 从图片库设置背景图像,c#,xaml,background,windows-phone-8,grid,C#,Xaml,Background,Windows Phone 8,Grid,我必须以编程方式设置一个网格的背景,其中包含从图片库(应用程序的“无资源”文件夹)拍摄的图像 我已尝试使用此代码 MediaLibrary library = new MediaLibrary(); Picture picture = library.Pictures[rnd.Next(0, 7)]; string path = picture.GetPath(); BackgroundImg.ImageSource = new BitmapImage { UriSource = new U
MediaLibrary library = new MediaLibrary();
Picture picture = library.Pictures[rnd.Next(0, 7)];
string path = picture.GetPath();
BackgroundImg.ImageSource = new BitmapImage { UriSource = new Uri("ms-appdata:///Local/" + picture.Name, UriKind.Absolute) };
//BackgroundImg.ImageSource = new BitmapImage { UriSource = new Uri(path, UriKind.Absolute) };
如果pict是从项目中的assets文件夹中获取的,那么代码就可以工作,所以我认为错误是uri
有人能帮我吗
Thanx为什么不使用StorageFile而不是“ms”-appdata:///local/“ 试试这个方法,我觉得很好
public void setBackgroundImage(String pictureName)
{
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = storage.OpenFile(pictureName, FileMode.Open))
{
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.SetSource(stream);
BackgroundImg.ImageSource = bitmapimage;
}
}
}
然后你叫它
setBackgroundImage(picture.Name)代码>
*注意:在Windows Phone 8中,您仍然可以使用独立存储和Silverlight Uri。我用这种方式解决了:
MediaLibrary library = new MediaLibrary();
Picture picture = library.Pictures[rnd.Next(0, library.Pictures.Count - 1)];
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.SetSource(picture.GetImage());
BackgroundImg.ImageSource = bitmapimage;
这似乎不起作用。。。我收到一个异常“不允许对IsolatedStorageFileStream执行操作”。我认为问题在于找不到映像。。。也许我应该移动到正确的文件夹(图片库)