Windows Phone - SavePicture - InvalidOperationException

时间:2011-09-28 19:34:12

标签: c# windows-phone-7 xna

如果我连接到PC,为什么此代码会抛出InvalidOperationException no metter?

MemoryStream ms = new MemoryStream();
picture.SaveAsJpeg(ms, 480, 800);
ms.Seek(0, SeekOrigin.Begin);
MediaLibrary l = new MediaLibrary();
l.SavePicture("test11", ms);
l.Dispose();
ms.Dispose();

我使用WP7 RC Tools和XNA 图片是Texture2D实例

3 个答案:

答案 0 :(得分:1)

刚刚解决了问题。

我忘记了我玩了权限(清单文件),并且意外删除了此权限

<Capability Name="ID_CAP_MEDIALIB" />

答案 1 :(得分:0)

在此处找到此示例:How to: Encode a JPEG for Windows Phone and Save to the Pictures Library

希望它有所帮助,它首先将流保存在IsolatedStorage中,然后从那里加载并最终保存在MediaLibrary中......

private void btnSave_Click(object sender, RoutedEventArgs e)
{
    // Create a file name for the JPEG file in isolated storage.
    String tempJPEG = "TempJPEG";

    // Create a virtual store and file stream. Check for duplicate tempJPEG files.
    var myStore = IsolatedStorageFile.GetUserStoreForApplication();
    if (myStore.FileExists(tempJPEG))
    {
        myStore.DeleteFile(tempJPEG);
    }

    IsolatedStorageFileStream myFileStream = myStore.CreateFile(tempJPEG);


    // Create a stream out of the sample JPEG file.
    // For [Application Name] in the URI, use the project name that you entered 
    // in the previous steps. Also, TestImage.jpg is an example;
    // you must enter your JPEG file name if it is different.
    StreamResourceInfo sri = null;
    Uri uri = new Uri("[Application Name];component/TestImage.jpg", UriKind.Relative);
    sri = Application.GetResourceStream(uri);

    // Create a new WriteableBitmap object and set it to the JPEG stream.
    BitmapImage bitmap = new BitmapImage();
    bitmap.CreateOptions = BitmapCreateOptions.None; 
    bitmap.SetSource(sri.Stream);
    WriteableBitmap wb = new WriteableBitmap(bitmap);

    // Encode the WriteableBitmap object to a JPEG stream.
    wb.SaveJpeg(myFileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
    myFileStream.Close();

    // Create a new stream from isolated storage, and save the JPEG file to the media library on Windows Phone.
    myFileStream = myStore.OpenFile(tempJPEG, FileMode.Open, FileAccess.Read);

    // Save the image to the camera roll or saved pictures album.
    MediaLibrary library = new MediaLibrary();

    if (radioButtonCameraRoll.IsChecked == true)
    {
        // Save the image to the camera roll album.
        Picture pic = library.SavePictureToCameraRoll("SavedPicture.jpg", myFileStream);
        MessageBox.Show("Image saved to camera roll album");
    }
    else
    {
        // Save the image to the saved pictures album.
        Picture pic = library.SavePicture("SavedPicture.jpg", myFileStream);
        MessageBox.Show("Image saved to saved pictures album");
    }

    myFileStream.Close();
}

答案 2 :(得分:0)

如果您已连接到PC,则无法使用MediaLibrary。而是连接WPConnect.exe

有关如何:Unable to launch CameraCaptureTask or PhotoChooserTask while debugging with device

的详细信息,请参阅此答案