我正在尝试从IsolatedStorage中保存图像。我收到错误:“IsolatedStorageFileStream上不允许操作。”。我的代码如下所示。我怎样才能克服这个问题?
public HomePage()
{
InitializeComponent();
// Create a filename for JPEG file in isolated storage.
String tempJPEG = "/Images/homescreenmap.png";
// Create virtual store and file stream. Check for duplicate tempJPEG files.
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists(tempJPEG))
{
myIsolatedStorage.DeleteFile(tempJPEG);
}
IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
StreamResourceInfo sri = null;
Uri uri = new Uri(tempJPEG, UriKind.Relative);
sri = Application.GetResourceStream(uri);
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(sri.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmap);
// Encode WriteableBitmap object to a JPEG stream.
Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
//wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
fileStream.Close();
}
}
答案 0 :(得分:0)
IsolatedStorageException意味着它无法找到您设置的位置路径,在您的情况下这是文件夹Images。只需在创建此代码的文件之前添加:
if (!myIsolatedStorage.DirectoryExists("Images"))
{
myIsolatedStorage.CreateDirectory("Images");
}