C# 如何将图片从媒体库保存到隔离存储
我需要将MediaLibrary中“已保存图片”相册中的5张最新图片保存到IsolatedStorage中。我不太确定完成这项任务的最佳方法。到目前为止,我正在媒体库中搜索“保存的图片”相册。如果相册存在并且相册中存在图片,我需要拍摄以文件名“TestApp”开头的最近5张图像,并将它们保存到IsolatedStorage。这些名称将用于更新磁贴,因此每个图像的文件路径都非常具体。到目前为止,我所知道的是,我不知道如何将C# 如何将图片从媒体库保存到隔离存储,c#,windows-phone-7,windows-phone-8,isolatedstorage,C#,Windows Phone 7,Windows Phone 8,Isolatedstorage,我需要将MediaLibrary中“已保存图片”相册中的5张最新图片保存到IsolatedStorage中。我不太确定完成这项任务的最佳方法。到目前为止,我正在媒体库中搜索“保存的图片”相册。如果相册存在并且相册中存在图片,我需要拍摄以文件名“TestApp”开头的最近5张图像,并将它们保存到IsolatedStorage。这些名称将用于更新磁贴,因此每个图像的文件路径都非常具体。到目前为止,我所知道的是,我不知道如何将p.GetImage()(它从System.IO.Stream类型的媒体库返
p.GetImage()
(它从System.IO.Stream类型的媒体库返回一个图像)保存到带有更新文件名的隔离存储中
private PictureCollection _pictures = null;
public void StoreCycleTileImages()
{
string _photoPath = @"\Shared\ShellContent";
string _photoFilename = null;
int i = 0;
using (MediaLibrary library = new MediaLibrary())
{
foreach (PictureAlbum album in library.RootPictureAlbum.Albums)
{
if (album.Name == "Saved Pictures")
{
_pictures = album.Pictures;
if(_pictures != null)
{
//search for the most recent pictures in the album with the correct file name
foreach (var p in _pictures.Reverse())
{
if (i >= 5)
return;
if (p.Name.Substring(0,7) == "TestApp")
{
i += 1;
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!storage.DirectoryExists(_photoPath))
{
storage.CreateDirectory(_photoPath);
}
//Update file name
_photoFilename = @"" + i.ToString();
if (storage.FileExists(_photoPath + @"\" + _photoFilename))
{
storage.DeleteFile(_photoPath + @"\" + _photoFilename);
}
//use p.GetImage() stream to save to IsolatedStorage with updated file name
//??
}
}
}
}
break;
}
}
}
}
<>我已经运行了你的代码——在创建文件名时,你错过了文件扩展名(你应该考虑它可以是JPG、PNG或其他图像)。我的工作代码(复制片段)如下所示:
photoFilename = @"" + i.ToString() + p.Name.Substring(p.Name.LastIndexOf('.'));
if (storage.FileExists(_photoPath + @"\" + _photoFilename))
{
storage.DeleteFile(_photoPath + @"\" + _photoFilename);
}
using (IsolatedStorageFileStream file = storage.CreateFile(_photoPath + @"\" + _photoFilename))
p.GetImage().CopyTo(file);