将图像保存到媒体库时无效操作异常



我正在尝试将图像保存到媒体库中,但是在调用SavePicture函数时出现"无效操作异常"。我有一个图像文件保存在隔离存储中,我想将其保存到图片中心。这是代码片段 -

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 在运行时阻止图像库有关。我已经停止了它,也没有手机连接到PC。当我在物理设备上测试此应用程序时,该应用程序只是崩溃,尽管我可以看到"保存的图片"中保存了空白图像。

我正在使用网络客户端从 url 下载图像,这似乎是正确的。Web客户端被传递图像的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中),因为当我稍后尝试打开它时,它已成功显示它。

您确定图像保存正确吗?MSDN 拥有将图片保存到媒体库的大量资源http://msdn.microsoft.com/en-us/library/ff769549(v=vs.92)

相关内容

最新更新