积极答复者
在使用IsolatedStorageFile时的一点问题

问题
-
在PhotoChooserTask对象的Completed事件里,我用IsolatedStorageFile实例化一个名为isf的对象,然后用FileExists方法判断名为"a.jpg"的图像是否存在与独立文件中,如果存在就用DeleteFile方法删除,然后根据选择的图片重新创建一个名为a.jpg的图像。代码如下:
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
WriteableBitmap bmp = new WriteableBitmap(435, 435);
bmp.SetSource(e.ChosenPhoto);
if (isf.FileExists("a.jpg"))
{
isf.DeleteFile("a.jpg");
}
using (Stream stream = isf.OpenFile("a.jpg", FileMode.OpenOrCreate))
{
Extensions.SaveJpeg(bmp, stream, 435, 435, 0, 100);
}
imageMain.Source = bmp;
}
}代码执行到isf.DeleteFile("a.jpg");报错An error occurred while accessing IsolatedStorage.
各位牛人们,求解啊····
答案
-
你好,我尝试了你的代码,运行并没有问题.
因为你报出的错误是An error occurred while accessing IsolatedStorage.似乎是Stream并没有通向IsolatedStorage
我对于你的代码进行了修改,希望你可以尝试下:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="Save_Click" /> <Button Content="Load" Height="72" HorizontalAlignment="Left" Margin="160,10,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="Load_Click" /> <Image Height="244" HorizontalAlignment="Left" Margin="12,88,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="438" /> <Image Height="244" HorizontalAlignment="Left" Margin="12,338,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="438" /> </Grid>
private void Save_Click(object sender, RoutedEventArgs e) { PhotoChooserTask task = new PhotoChooserTask(); task.Completed += new EventHandler<PhotoResult>(task_Completed); task.Show(); } void task_Completed(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); WriteableBitmap bmp = new WriteableBitmap(435, 435); bmp.SetSource(e.ChosenPhoto); if (isf.FileExists("a.jpg")) { isf.DeleteFile("a.jpg"); } using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("a.jpg",FileMode.Create,isf)) { Extensions.SaveJpeg(bmp, isfs, 435, 435, 0, 100); isfs.Flush(); } image1.Source = bmp; } } private void Load_Click(object sender, RoutedEventArgs e) { IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); WriteableBitmap bmp = new WriteableBitmap(435, 435); if (isf.FileExists("a.jpg")) { using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("a.jpg", FileMode.Open, isf)) { Extensions.LoadJpeg(bmp, isfs); isfs.Flush(); } } image2.Source = bmp; }
- 已标记为答案 Otomii Lu 2012年4月5日 2:04
全部回复
-
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); //isf定义
//保存图片
WriteableBitmap bmp = new WriteableBitmap(435, 435);
bmp.SetSource(e.ChosenPhoto);using (Stream stream = isf.OpenFile("Puzzle.jpg", FileMode.OpenOrCreate))
{
Extensions.SaveJpeg(bmp, stream, 435, 435, 0, 100);
} -
你好,我尝试了你的代码,运行并没有问题.
因为你报出的错误是An error occurred while accessing IsolatedStorage.似乎是Stream并没有通向IsolatedStorage
我对于你的代码进行了修改,希望你可以尝试下:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="Save_Click" /> <Button Content="Load" Height="72" HorizontalAlignment="Left" Margin="160,10,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="Load_Click" /> <Image Height="244" HorizontalAlignment="Left" Margin="12,88,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="438" /> <Image Height="244" HorizontalAlignment="Left" Margin="12,338,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="438" /> </Grid>
private void Save_Click(object sender, RoutedEventArgs e) { PhotoChooserTask task = new PhotoChooserTask(); task.Completed += new EventHandler<PhotoResult>(task_Completed); task.Show(); } void task_Completed(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); WriteableBitmap bmp = new WriteableBitmap(435, 435); bmp.SetSource(e.ChosenPhoto); if (isf.FileExists("a.jpg")) { isf.DeleteFile("a.jpg"); } using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("a.jpg",FileMode.Create,isf)) { Extensions.SaveJpeg(bmp, isfs, 435, 435, 0, 100); isfs.Flush(); } image1.Source = bmp; } } private void Load_Click(object sender, RoutedEventArgs e) { IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); WriteableBitmap bmp = new WriteableBitmap(435, 435); if (isf.FileExists("a.jpg")) { using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("a.jpg", FileMode.Open, isf)) { Extensions.LoadJpeg(bmp, isfs); isfs.Flush(); } } image2.Source = bmp; }
- 已标记为答案 Otomii Lu 2012年4月5日 2:04