我正在尝试将图像保存到媒体库,但在调用 SavePicture 函数时出现“InvalidOperationException”。我有一个保存在隔离存储中的图像文件,我想保存到图片中心。这是代码片段 -
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(fname, FileMode.Open, FileAccess.Read)) //fname is the filename of the image that is to be saved in the library
{
MediaLibrary mediaLibrary = new MediaLibrary();
Picture pic = mediaLibrary.SavePicture("SavedLogo.jpg", fileStream); //Exception thrown here!
fileStream.Close();
}
}
从我收集到的信息来看,这个异常与 Zune 在运行时阻止了图像库有关。我已经停止了,也没有手机连接到电脑。当我在物理设备上测试这个应用程序时,应用程序只是崩溃了,虽然我可以看到保存在“保存的图片”中的空白图像。
我正在使用网络客户端从 url 下载图像,它似乎是正确的。网络客户端通过图像的 url (openReadAsync)。这是 openReadCompleted 事件:
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var resInfo = new StreamResourceInfo(e.Result, null);
var reader = new StreamReader(resInfo.Stream);
byte[] contents;
using (BinaryReader bReader = new BinaryReader(reader.BaseStream))
{
contents = bReader.ReadBytes((int)reader.BaseStream.Length);
}
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = isf.CreateFile("example.jpg");
stream.Write(contents, 0, contents.Length);
stream.Close();
}
我认为它正在正确保存文件(即在隔离的sotrage 中),因为当我稍后尝试打开它时它会成功显示它。