单击按钮上的图像保存会在WP7中抛出未处理的异常错误,并且还会保存重复的图像

时间:2011-01-29 10:39:42

标签: c# silverlight windows-phone-7

我想点击按钮在WP7手机中保存图像 以下是代码

 private void wallpaper_Click(object sender, RoutedEventArgs e)
    {

        String imageName ="temjpeg";      I

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

        IsolatedStorageFileStream myFileStream = myStore.CreateFile(imageName);


        // 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 filename if it is different.
        StreamResourceInfo sri = null;
        Uri uri = new Uri("some String",UriKind.Relative);
        sri = Application.GetResourceStream(uri);

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


        // Encode WriteableBitmap object to a JPEG stream.
        // SaveJpeg(WriteableBitmap bitmap, Stream targetStream, int targetWidth, int targetHeight, int orientation, int quality)
        Extensions.SaveJpeg(wb, 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(temjpeg, FileMode.Open, FileAccess.Read);

        MediaLibrary library = new MediaLibrary();

         Picture pic = library.SavePicture(imageName, myFileStream);
        myFileStream.Close();
         }

但是这段代码在行上抛出InvalidOperationException:Picture pic = library.SavePicture(imageName,myFileStream); 可能是什么问题呢?.. 如果异常问题解决了,现在如果多次点击保存按钮,图像会多次保存。有人帮忙吗?

1 个答案:

答案 0 :(得分:1)

您是否在模拟器上执行此操作,因为SavePicture无法在模拟器上运行,只能在实际的手机上运行。