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;仅当你要把图片保存到媒体库的时候才需要添加引用。

对于很多应用,向隔离存储空间读取、保存图片文件是很常见的任务。在WP7中,你还可以保存、读取媒体库中的图片。

一般情况下我们使用类IsolatedStorageFileStream进行读、写、创建文件等操作。对于图片,最大的不同就是使用类BitmapImage和类WriteableBitmap.


保存Image:

 private void btSaveImage_Click(object sender, RoutedEventArgs e)
        {
            String strTempJPEG = "iamge111.png";  
            using(IsolatedStorageFile iso=IsolatedStorageFile.GetUserStoreForApplication ())
            {
                if (iso.FileExists (strTempJPEG ))
                {
                    iso.DeleteFile(strTempJPEG );
                }
                using(IsolatedStorageFileStream isostream=iso.CreateFile(strTempJPEG))
                {
                    StreamResourceInfo sri = null;
                    Uri uri = new Uri(strTempJPEG ,UriKind.Relative);
                    sri = Application.GetResourceStream(uri);


                    BitmapImage bitmap = new BitmapImage();
                    bitmap.SetSource(sri.Stream );
                    WriteableBitmap wb = new WriteableBitmap(bitmap);
                    Extensions.SaveJpeg(wb ,isostream,wb.PixelWidth,wb.PixelHeight,0,85);
                    isostream.Close();
                }
            }


        }

读取Image:

 private void btScanImage_Click(object sender, RoutedEventArgs e)
        {
            BitmapImage bitmap = new BitmapImage();
            using(IsolatedStorageFile iso=IsolatedStorageFile.GetUserStoreForApplication ())
            {
                using(IsolatedStorageFileStream isostream=iso.OpenFile ("iamge111.png",FileMode.Open ,FileAccess.Read ))
                {
                    bitmap.SetSource(isostream);
                    this.image.Height = bitmap.PixelHeight;
                    this.image.Width = bitmap.PixelWidth;
                }
            }
            this.image.Source = bitmap;
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值