[英]How to Save Pictures from MediaLibrary to IsolatedStorage
I have the requirement to save the 5 most recent pictures from the 'Saved Pictures' album from MediaLibrary into IsolatedStorage. 我需要将MediaLibrary的“ Saved Pictures”相册中的5张最新图片保存到IsolatedStorage中。 I am not quite sure the best way to accomplish this task. 我不太确定完成此任务的最佳方法。 So far I am searching through the MediaLibrary for the 'Saved Pictures' album. 到目前为止,我正在MediaLibrary中搜索“ Saved Pictures”专辑。 If the album exists and pictures exist within that album, I need to take the most recent 5 images that start with the filename "TestApp" and save them to IsolatedStorage. 如果相册存在并且该相册中存在图片,则需要拍摄以文件名“ TestApp”开头的最新5张图像,并将它们保存到IsolatedStorage。 The names will be used to update a tile, so the filepath of each image is very specific. 名称将用于更新图块,因此每个图像的文件路径非常具体。 What I have so far is as follows, I'm jus tnot sure how to save p.GetImage()
(which returns an image from the MediaLibrary of type System.IO.Stream
) to IsolatedStorage with the updated filename 到目前为止,我所拥有的如下,我不确定如何将p.GetImage()
(从System.IO.Stream
类型的MediaLibrary返回的图像)保存到具有更新文件名的IsolatedStorage中。
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;
}
}
}
}
I've run your code - you miss the file extension while creating file name (you should consider that it can be jpg, png, or other image). 我运行了您的代码-创建文件名时您错过了文件扩展名(您应该考虑它可以是jpg,png或其他图像)。 My working code (copying fragment) looks like this: 我的工作代码(复制片段)如下所示:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.