将图像保存在Windows手机中

时间:2012-10-29 16:15:53

标签: c# windows-phone-7 isolatedstorage

每当我尝试保存文件时,我都会收到此错误 -

   1. System.IO.IsolatedStorage.IsolatedStorageException was unhandled  
        Message=Operation not permitted on IsolatedStorageFileStream.  
        StackTrace:
               at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String
        path, FileMode mode, FileAccess access, FileShare share, Int32
        bufferSize, IsolatedStorageFile isf)
               at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String
        path, FileMode mode, FileAccess access, IsolatedStorageFile isf)
               at System.IO.IsolatedStorage.IsolatedStorageFile.OpenFile(String path,
        FileMode mode, FileAccess access)
               at PaintBrush.Save.savepic()
               at PaintBrush.Save..ctor()
               at PaintBrush.MainPage.click_btnSave(Object sender, RoutedEventArgs e)
               at System.Windows.Controls.Primitives.ButtonBase.OnClick()
               at System.Windows.Controls.Button.OnClick()
               at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs
        e)
               at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl,
        EventArgs e)
               at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32
        actualArgsTypeIndex, String eventName)

我正在尝试保存图片。我已经通过谷歌并发现他们说要在写入之前关闭文件流但是我无法得到它。因为我在写入之前关闭文件流时遇到了同样的错误。这是我的代码 -

public  void savepic()
        {
            string filename=DateTime.Today.ToString()+".jpg";

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(filename, FileMode.Open, FileAccess.ReadWrite))
                {
                    MediaLibrary mediaLibrary = new MediaLibrary();
                    Picture pic = mediaLibrary.SavePicture(filename, fileStream);
                    fileStream.Close();
                }
            }

            PhotoChooserTask photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Show();
        }

2 个答案:

答案 0 :(得分:1)

这是我的代码片段,可能对您有帮助。

我认为您的问题在于OpenFile方法。

 // Create a filename for JPEG file in isolated storage.
 String tempJPEG = "logo.jpg";
 // Create virtual store and file stream.
 using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
 {

     IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);

  // Encode WriteableBitmap object to a JPEG stream.
     Extensions.SaveJpeg(img, fileStream, img.PixelWidth, img.PixelHeight, 0, 85);
     fileStream.Close();
   }

答案 1 :(得分:0)

MessageBar.Text = "Uploading to SkyDrive";
IsolatedStorageFileStream fileStream = null;
//string strSaveName = "images.jpg";               
try
{
   using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
   {
      fileStream = store.OpenFile("contact.txt", FileMode.Open, FileAccess.Read);
   }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
client.UploadAsync("me/SkyDrive", "contact.txt", fileStream, OverwriteOption.Overwrite);