Windows Phone - SavePicture - InvalidOperationException

发布于 2024-12-07 05:21:48 字数 322 浏览 2 评论 0原文

为什么无论我是否连接到 PC,这段代码都会抛出 InvalidOperationException?

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 工具和 XNA 图片是一个Texture2D实例

Why this code throws InvalidOperationException no metter if I am conncted to PC or not?

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();

I use WP7 RC Tools and XNA
picture is an Texture2D instance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你对谁都笑 2024-12-14 05:21:48

刚刚解决了问题。

我忘了我玩过权限(清单文件),不小心删除了这个权限

<Capability Name="ID_CAP_MEDIALIB" />

Just solved the problem.

I forgot I've played with permissions (manifest file), and accidentaly deleted this permission

<Capability Name="ID_CAP_MEDIALIB" />
_畞蕅 2024-12-14 05:21:48

在这里找到这个示例:如何:编码 JPEG对于Windows Phone并保存到图片库

希望它有帮助,它首先将流保存在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();
}

found this example here: How to: Encode a JPEG for Windows Phone and Save to the Pictures Library

hopefully it helps, it is saving the stream in the IsolatedStorage first then loading from there and saving in the MediaLibrary in the end...

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();
}
一腔孤↑勇 2024-12-14 05:21:48

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

有关如何连接的详细信息,请参阅此答案:使用设备进行调试时无法启动 CameraCaptureTask 或 PhotoChooserTask

If you're connected to the PC, you can't use the MediaLibrary. Instead connect with WPConnect.exe

See this answer for details on how: Unable to launch CameraCaptureTask or PhotoChooserTask while debugging with device

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文