(4)[wp7数据存储] WP7 IsolatedStorage系列篇--读取、保存图片文件 [复制链接]

 

本帖最后由 agameboy 于 2012-5-17 17:08 编辑

对于很多应用,向隔离存储空间读取、保存图片文件是很常见的任务。在WP7中,你还可以保存、读取媒体库中的图片。更多的文章请参考WP7 IsolatedStorage系列篇
引用命名空间:
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using Microsoft.Phone.Tasks;
using Microsoft.Xna.Framework.Media;

关心:Microsoft.Xna.Framework.Media;仅当你要把图片保存到媒体库的时候才需要添加引用。
一般情况下我们使用类IsolatedStorageFileStream进行读、写、创建文件等操作。对于图片,最大的不同就是使用类BitmapImage和类WriteableBitmap.

保存Image:

  1. private void btSaveImage_Click(object sender, RoutedEventArgs e)
  2.         {
  3.             String strTempJPEG = "iamge111.png";  
  4.             using(IsolatedStorageFile iso=IsolatedStorageFile.GetUserStoreForApplication ())
  5.             {
  6.                 if (iso.FileExists (strTempJPEG ))
  7.                 {
  8.                     iso.DeleteFile(strTempJPEG );
  9.                 }
  10.                 using(IsolatedStorageFileStream isostream=iso.CreateFile(strTempJPEG))
  11.                 {
  12.                     StreamResourceInfo sri = null;
  13.                     Uri uri = new Uri(strTempJPEG ,UriKind.Relative);
  14.                     sri = Application.GetResourceStream(uri);
  15.                     BitmapImage bitmap = new BitmapImage();
  16.                     bitmap.SetSource(sri.Stream );
  17.                     WriteableBitmap wb = new WriteableBitmap(bitmap);
  18.                     Extensions.SaveJpeg(wb ,isostream,wb.PixelWidth,wb.PixelHeight,0,85);
  19.                     isostream.Close();
  20.                 }
  21.             }
  22.         }
复制代码


读取Image:

  1. private void btScanImage_Click(object sender, RoutedEventArgs e)
  2.         {
  3.             BitmapImage bitmap = new BitmapImage();
  4.             using(IsolatedStorageFile iso=IsolatedStorageFile.GetUserStoreForApplication ())
  5.             {
  6.                 using(IsolatedStorageFileStream isostream=iso.OpenFile ("iamge111.png",FileMode.Open ,FileAccess.Read ))
  7.                 {
  8.                     bitmap.SetSource(isostream);
  9.                     this.image.Height = bitmap.PixelHeight;
  10.                     this.image.Width = bitmap.PixelWidth;
  11.                 }
  12.             }
  13.             this.image.Source = bitmap;
  14.         }
复制代码
posted @ 2012-11-29 14:33  BellingWP  阅读(179)  评论(0编辑  收藏  举报